Homer5 Posted October 6, 2014 Share Posted October 6, 2014 this is my solution: $x = ['a' => 2, 'b' => 1, 'c' => 4, 'd' => 1]; $x1 = array_keys($x); $x2 = array_values($x); $z = []; for ($i=0; $i<count($x); $i++) { array_push($z, [$x1[$i],$x2[$i]] ); } print_r($z); //[['a', 2], ['b', 1], ['c', 4], ['d', 1]]; is there easier (more elegant) way to do the same ? Link to comment https://forums.phpfreaks.com/topic/291469-convert-associative-array-to-multidimensional/ Share on other sites More sharing options...
Barand Posted October 6, 2014 Share Posted October 6, 2014 foreach ($x as $k => $v) { $z[] = [$k,$v]; } Link to comment https://forums.phpfreaks.com/topic/291469-convert-associative-array-to-multidimensional/#findComment-1492857 Share on other sites More sharing options...
Homer5 Posted October 6, 2014 Author Share Posted October 6, 2014 yep, that's much better ... thanks Link to comment https://forums.phpfreaks.com/topic/291469-convert-associative-array-to-multidimensional/#findComment-1492860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.