php - Converting a non-associative list of arrays to an associative array of arrays -


i have list of arrays (or objects, coming database via pdo fetchall() function, both options ok me). wish convert list of arrays associative array of arrays key of each array being 1 of columns.

i can loop, wondering whether there php function this, maybe in more efficient way.

so illustrate it, lets have array (non-associative) arrays inside:

[0] => {'name' : 'joe', 'surname' : 'bloggs', 'id' : '12345'} [1] => {'name' : 'sandy', 'surname' : 'smith', 'id' : '54321'} 

i wish convert to:

['12345'] => {'name' : 'joe', 'surname' : 'bloggs', 'id' : '12345'} ['54321'] => {'name' : 'sandy', 'surname' : 'smith', 'id' : '54321'} 

a simple loop do, that's boring post answer for, here go:

$array = array_combine(array_map(function (array $row) { return $row['id']; }, $array),                        $array); 

if you're partial functional php:

$array = array_combine(f\pluck($array, 'id'), $array); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -