0

I have a list of arrays (or objects, they are coming from a database via the PDO fetchAll() function, so both options are OK for me). I wish to convert the list of arrays to an associative array of arrays with the key of each array being one of its columns.

I can obviously do a loop, but I am wondering whether there is some PHP function which already does this, maybe in a more efficient way.

So to illustrate it, lets say I have an array (non-associative) with arrays inside:

[0] => {'name' : 'Joe', 'surname' : 'Bloggs', 'id' : '12345'}
[1] => {'name' : 'Sandy', 'surname' : 'Smith', 'id' : '54321'}

I wish to convert it to:

['12345'] => {'name' : 'Joe', 'surname' : 'Bloggs', 'id' : '12345'}
['54321'] => {'name' : 'Sandy', 'surname' : 'Smith', 'id' : '54321'}
2
  • Do you have objects or arrays ? Commented Apr 17, 2013 at 23:00
  • @adeneo I have associative arrays (internally) at the moment, but I can change them easily to objects if needed. Just a question of changing to PDO::FETCH_OBJ. So both options OK for me if an elegant technique exists (which does not involve looping). Commented Apr 17, 2013 at 23:06

2 Answers 2

6

A simple loop would do, but that's too boring to post an answer for, so here you go:

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

If you're partial to functional PHP:

$array = array_combine(F\pluck($array, 'id'), $array);
Sign up to request clarification or add additional context in comments.

9 Comments

LOL :), well yeah not looking for a simple loop which I can easily do. Do you think this is more efficient?
No, since it 1) loops over the array twice and 2) stores a separate array of ids in memory. You could optimize that with a loop. Probably doesn't make a lot of difference for average array sizes though.
As far as I know the array_map function needs a "callable" as first param, and an array is not..
@arraintxo Dayum, right. The argument order keeps tripping me up. :)
@deceze Functional PHP? Thats not standard PHP right? Interesting though!
|
-1
$rows = $stmt->fetch(PDO::FETCH_ASSOC);

or

$rows = $stmt->fetchall(PDO::FETCH_ASSOC);

Let's read what HashPHP said:

Note the use of PDO::FETCH_ASSOC in the fetch() and fetchAll() code above. This tells PDO to return the rows as an associative array with the field names as keys. Other fetch modes like PDO::FETCH_NUM returns the row as a numerical array. The default is to fetch with PDO::FETCH_BOTH which duplicates the data with both a numerical and associative keys. It's recommended you specify one or the other so you don't have arrays that are double the size! PDO can also fetch objects with PDO::FETCH_OBJ, and can take existing classes with PDO::FETCH_CLASS. It can also bind into specific variables with PDO::FETCH_BOUND and using bindColumn method.

3 Comments

How will it know which column I want to be the key in the associative array? I think that works on the internal individual arrays (which I am already doing) I am looking at the outer array.
this will produce his 1st array, not the one he wants!
As @michi says that would not return the array he wants, just the one he allready has.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.