php - manipulate a multidimensional array -


i have mysql query returns me in php array this

array(     [0] -> ([cred_id],[date],[value]);     [1] -> ([cred_id],[date],[value]);     [2] -> ([cred_id],[date],[value]);     [3] -> ([cred_id],[date],[value]);     [4] -> ([cred_id],[date],[value]);     [5] -> ([cred_id],[date],[value]);     [6] -> ([cred_id],[date],[value]);     [7] -> ([cred_id],[date],[value]);     [8] -> ([cred_id],[date],[value]); ); 

and on. cred_id can same different array items. i'd turn array this:

array( [cred_id]->(([date],[value]),([date],[value]),([date],[value])) [cred_id]->(([date],[value]),([date],[value])) ); 

this way if know [cred_id] value can foreach loop between date-value pair have extracted database.

is there smart php function can use or have go foreach this:

foreach ($creds $cred){     $new_creds[$cred['cred_id']][]= array($cred[date],$cred[value]); }    

that smartest idea on how result?

other looping using foreach can make use of array_walk or array_map function below

demo

$output1 = array();  array_walk($array,function($item,$key) use (&$output1){      $output1[$item['cred_id']][]=array($item['date'],$item['value']);      });  // or    $output2 = array(); array_map(function($item) use (&$output2){      $output2[$item['cred_id']][]=array($item['date'],$item['value']); },$array);  print_r($output1); print_r($output2); 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -