Jump to content

convert associative array to multidimensional


Homer5

Recommended Posts

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 ?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.