13

What's the easiest way to convert

$a = array(
    array("id" => 1, "name" => "a1"),
    array("id" => 2, "name" => "a2")
);

to

$b = array(
    "a1" => array("id" => 1, "name" => "a1"),
    "a2" => array("id" => 2, "name" => "a2")
);

I was expecting PHP have some functional programming facilities to do something like:

$b = map($a, function($item) {
    return $item["name"];
});

But I didn't find one.

4

6 Answers 6

23

You can use PHP array_column() function. For your use case, the second argument should be null to return the full array.

$a = array(
array("id" => 1, "name" => "a1"),
array("id" => 2, "name" => "a2")
);

$b = array_column($a, null, 'name');

and print_r($b) will result in

Array
(
[a1] => Array
    (
        [id] => 1
        [name] => a1
    )

[a2] => Array
    (
        [id] => 2
        [name] => a2
    )

)
Sign up to request clarification or add additional context in comments.

2 Comments

upvote, more straight forward solution, but PHP7 only.
I think is php 5.5 above for array_column, not php7
8

The only solution I see is to loop through the array and create a new array manually.

For example, like this:

$new_array = [];
foreach ($array as $value) 
    $new_array[$value['name']] = $value;

3 Comments

Thanks, I might be influenced by other languages too much. The problem is very common, but I didn't find a built-in solution for it. Yours is the easiest way I could think as well so far.
Glad I could help. It might also be good to check if $value['name'] exists otherwise the code might crash in certain situations.
OK, they are basically objects loaded from one table of database.
1

The accepted answer is close, but "close" only counts with hand grenades.

($unique is an indexed array)

$sub_key = 'country';
$new_array = [];
foreach ($unique as $value) {
  $new_array[] = [$sub_key => $value];
}

Comments

0

Poor me need to implement this in PHP 5.2, and I do not want to use for loop

here's my implementation:

        function getField($x) { return $x['name']; }
        $ids = array_map('getField', $result);
        $result = array_combine($ids, $result);

See if maybe this helps anybody lol

Comments

0

How about:

$a = array(
    array('a' => 'a0', 'b' => 'b0', 'c' => 'c0'),
    array('a' => 'a1', 'b' => 'b1', 'c' => 'c1'),
    array('a' => 'a2', 'b' => 'b2', 'c' => 'c2'),
    );
print_r($a);
print_r(array_combine(array_column($a, 'a'), $a));

Where 'a' is the column to use as the associative index.

This leaves the column that becomes the index in the array.

array_column (PHP 5 >= 5.5.0, PHP 7)

array_combine (PHP 5, PHP 7)

Comments

0

You can simply do this

$input = [
    ["id" => 1, "name" => "a1"],
    ["id" => 2, "name" => "a2"]
];

$result = array_merge(
    ...array_map(
        fn($item) => [$item['name'] => $item],
        $input
    )
);

1 Comment

Adding some explanation to your code would make this answer more helpful. Explaining how it improves on the one-line answer given almost 5 years ago would also be a good idea.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.