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 ? Quote Link to comment https://forums.phpfreaks.com/topic/291469-convert-associative-array-to-multidimensional/ Share on other sites More sharing options...
Solution Barand Posted October 6, 2014 Solution Share Posted October 6, 2014 foreach ($x as $k => $v) { $z[] = [$k,$v]; } 1 Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.