bannock Posted August 10, 2009 Share Posted August 10, 2009 Hi, I have a database query returning the following array structure; Array ( [0] => Array ( [Category] => Array ( [id] => 9 [name] => ABC ) [Ds] => Array ( [ds_id] => 39 ) ) [1] => Array ( [Category] => Array ( [id] => 10 [name] => DEF ) [Ds] => Array ( [ds_id] => 40 ) ) ) I need to change the structure of the array(to feed a json object) to this; Array ( [0] => Array ( [id] => 9 [name] => ABC [ds_id] => 39 ) [1] => Array ( [id] => 10 [name] => DEF [ds_id] => 40 ) ) I am embarassed to say i have spent the best part of two days mucking around with a multitude of recursive functions to no avail. At best i completely flatten the array at worst i end up with a complete mishmash. I tried to set up a php debugger to watch the process but after 3 days of trying i came to the conclusion it was not compatible with my working environment. I am still using php 4.4.9. Any assistance would be greatly appreciated Link to comment https://forums.phpfreaks.com/topic/169560-converting-a-multi-dimensional-array-structure/ Share on other sites More sharing options...
scvinodkumar Posted August 10, 2009 Share Posted August 10, 2009 could u please post your php code here, we will give u correct format as u like the way Link to comment https://forums.phpfreaks.com/topic/169560-converting-a-multi-dimensional-array-structure/#findComment-894619 Share on other sites More sharing options...
trq Posted August 10, 2009 Share Posted August 10, 2009 for ($i=0;$i<=count($array);$i++) { $array[$i] = array_merge($array[$i]['Category'],$array[$i]['Ds']); } Link to comment https://forums.phpfreaks.com/topic/169560-converting-a-multi-dimensional-array-structure/#findComment-894621 Share on other sites More sharing options...
halfman Posted August 10, 2009 Share Posted August 10, 2009 Try this I have got this from http://us3.php.net/manual/en/function.array-values.php <?php function array_flatten($array, $preserve = FALSE, $r = array()){ foreach($array as $key => $value){ if (is_array($value)){ foreach($value as $k => $v){ if (is_array($v)) { $tmp = $v; unset($value[$k]); } } if ($preserve) $r[$key] = $value; else $r[] = $value; } } $r = isset($tmp) ? array_flatten($tmp, $preserve, $r) : $r; return $r; } print_r($tmp); /* --- Array ( [home] => Array ( [id] => 1 [pid] => 0 [link] => home [subcat] => ) [works] => Array ( [id] => 2 [pid] => 0 [link] => works [subcat] => Array ( [viz] => Array ( [id] => 4 [pid] => 2 [link] => viz [subcat] => ) [script] => Array ( [id] => 5 [pid] => 2 [link] => script [subcat] => Array ( [arch] => Array ( [id] => 6 [pid] => 5 [link] => arch [subcat] => ) ) ) ) ) [blog] => Array ( [id] => 3 [pid] => 0 [link] => blog [subcat] => ) ) --- */ print_r(array_flatten($tmp, 1)); /* --- Array ( [home] => Array ( [id] => 1 [pid] => 0 [link] => home [subcat] => ) [works] => Array ( [id] => 2 [pid] => 0 [link] => works ) [blog] => Array ( [id] => 3 [pid] => 0 [link] => blog [subcat] => ) [viz] => Array ( [id] => 4 [pid] => 2 [link] => viz [subcat] => ) [script] => Array ( [id] => 5 [pid] => 2 [link] => script ) [arch] => Array ( [id] => 6 [pid] => 5 [link] => arch [subcat] => ) ) --- */ ?> Link to comment https://forums.phpfreaks.com/topic/169560-converting-a-multi-dimensional-array-structure/#findComment-894624 Share on other sites More sharing options...
bannock Posted August 10, 2009 Author Share Posted August 10, 2009 thanks for the quick replies; I failed to mention, the data is dynamic, thus identifying the keys in the array for the filtering process may not be practical. The code from the php manual flattens the array completely. I will post some code, but i have made so many variations to it that i usually delete and start afresh. Link to comment https://forums.phpfreaks.com/topic/169560-converting-a-multi-dimensional-array-structure/#findComment-894630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.