php - Create assoc array from array of asoc array -


how can create new array structure: [id] => [prop] data this

$foo = array(     array('id' => 2, 'prop' => 'val2'),     array('id' => 1, 'prop' => 'val1'),     array('id' => 3, 'prop' => 'val3'), ); 

but in elegant way, without foreach loop?

in php 5.5:

$result = array_column($foo, 'prop', 'id'); 

for php<5.5:

$result = array_combine(    array_map(function($x)    {       return $x['id'];     }, $foo),    array_map(function($x)    {       return $x['prop'];     }, $foo), ); 

i have note, despite hidden inside callbacks or built-in functions, it's still loop inside - thus, in terms of complexity it's same plain loop.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -