Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the content of an array to be more efficient [duplicate]

Tags:

arrays

php

When doing a print_r on my array, I get the following output;

Array
(
    [0] => Array
        (
            [id] => 178
            [name] => Briar Price
        )

    [1] => Array
        (
            [id] => 90
            [name] => Bradley Kramer
        )

    [2] => Array
        (
            [id] => 508
            [name] => Calvin Yang
        )

    [3] => Array
        (
            [id] => 457
            [name] => Charles Valenzuela
        )

    ... and so on

How can I modify the array to look like this;

Array
(
    [178] => Briar Price
    [90] => Bradley Kramer
    [508] => Calvin Yang
    [457] => Charles Valenzuela
    ... and so on
)

I just want to make the ID the key for the value name. I always have issues when it comes to arrays reordering.

like image 974
Mentos93 Avatar asked Dec 06 '22 17:12

Mentos93


1 Answers

Pass third parameter to array_column to make its key as

$array = array_column($users, 'name', 'id');

print_r($array);
like image 123
Saty Avatar answered Dec 09 '22 14:12

Saty