Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to form an multidimensional array as below format however i could not figure out the algorithm

array(
      [0]=>array(
            "id"=>"1","data"=>array([0]=>array("kid"=>"434","k"=>"Ali","m"=>"msj1"), [1]=>array("kid"=>"344","k"=>"Dali","m"=>"msj3")),
      [1]=>array(
            "id"=>"2","data"=>array([0]=>array("kid"=>"347","k"=>"Cenk","m"=>"msj2"), [1]=>array("kid"=>"345","k"=>"Tan","m"=>"msj4")))

the data comes from mysql query like below:

SELECT kid, k, m, id FROM table1 WHERE rid=1 ORDER BY (id)

sample data:

id   kid   k    m
1    434  Ali  msj1
2    347  Cenk msj2
1    344  Dali msj3
2    345  Tan  msj4

php loop is as below:

do {
//whatever i tried here failed :(
} while ($t = mysql_fetch_assoc($r_tav));

i hope that i will understand multidimensional arrays better with this sample

share|improve this question
do-while is not your friend here as you will not be able to access the $t array in the loop. – Narcis Radu Nov 18 '11 at 10:38
Ali Aktaş, edit example array please, so i can help better – Utku Yıldırım Nov 18 '11 at 10:42
i think there should be a dummy variable to check if "id" change or not. depending on the change of id a new array to hold other data arrays should be formed. But some how i cant write down the code – aliaktas Nov 18 '11 at 11:01

2 Answers

up vote 4 down vote accepted
$arrRows = mysql_fetch_array($r_tav);
$arrRowData = array();
$arrRowDataFinal = array();
foreach ($arrRows as $key => $value){
    $arrRowData[$value['id']][] = array("kid"=>$value['kid'],"k"=>$value['k'],"m"=>$value['m']);
}
foreach($arrRowData as $key => $value){
    $arrRowDataFinal[] = array('id' => $key, 'data' => $value);
}
share|improve this answer
i tried the code, i generates the arrays in right format however it fails to collect data in common "id" arrays. i will work on to generate correct output – aliaktas Nov 18 '11 at 11:16
your solution is correct one, except we need to form the array of raw data in to $arrRows.. i do that with code of $t = mysql_fetch_assoc($r_tav); $row_sayi = mysql_num_rows($r_tav); if ($row_sayi > 0) { do { $tav_a[]=$t; $op=$t; } while ($t = mysql_fetch_assoc($r_tav)); } $arrRows = $tav_a; betters ways of generating above array is welcome.. tahnks Poonam, thanks stackoverflow – aliaktas Nov 18 '11 at 11:31

It seemed like all you want is:

while ($t = mysql_fetch_assoc($r_tav)){
    $arr[] = $t;
}

and $arr will contain your array

After re-reading the question, it seems that it is not exactly the structure you want, but it can simply be turned into:

while ($t = mysql_fetch_assoc($r_tav)){
    $arr[] = array('id' => $r_tav['id'],
                   'data' => array('kid' => $r_tav['kid'],
                                    'k' => $r_tav['k'],
                                    'm' => $r_tav['m']));
}
share|improve this answer
i wish it could be such simple however as you can see there are nested arrays including data related with the same id – aliaktas Nov 18 '11 at 10:41
i updated the post with a snippet that should generate the correct array – Jan Dragsbaek Nov 18 '11 at 10:48
this one is improved however how can i collect the data with the same id inside one array named "data" ? – aliaktas Nov 18 '11 at 10:51
that is beyond the original question, and adds a new layer of complexity – Jan Dragsbaek Nov 18 '11 at 11:21
it is my mistake to fully wrote down the final array, now we have a solution. hope that it will help others with the same need. thanks for your effort – aliaktas Nov 18 '11 at 11:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.