4

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

3
  • do-while is not your friend here as you will not be able to access the $t array in the loop. Commented Nov 18, 2011 at 10:38
  • Ali Aktaş, edit example array please, so i can help better Commented Nov 18, 2011 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 Commented Nov 18, 2011 at 11:01

2 Answers 2

4
$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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
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
4

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']));
}

5 Comments

i wish it could be such simple however as you can see there are nested arrays including data related with the same id
i updated the post with a snippet that should generate the correct array
this one is improved however how can i collect the data with the same id inside one array named "data" ?
that is beyond the original question, and adds a new layer of complexity
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.