4

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.

Mentos93
  • 581
  • 1
  • 7
  • 28

4 Answers4

6

Pass third parameter to array_column to make its key as

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

print_r($array);
Saty
  • 22,443
  • 7
  • 33
  • 51
  • 1
    Sam answer: http://stackoverflow.com/a/37919187/3933332 a few days ago. – Rizier123 Jul 12 '16 at 10:01
  • For some reason I get an error when trying to do it this way; `Attempted to call function "array_column" from namespace my\namespace` – Mentos93 Jul 12 '16 at 10:03
  • 1
    @Mentos93 try adding a slash in front: `\array_column(...)` – BeetleJuice Jul 12 '16 at 10:08
  • @Saty I won't link to the exact same answer from 4 days ago: http://stackoverflow.com/a/38264062 because that doesn't yield the exact output from OP's question there. – Rizier123 Jul 12 '16 at 10:08
  • Adding a slash give the same error but from the global namespace. But it's ok, I understand what's going on here. – Mentos93 Jul 12 '16 at 10:12
3

Use foreach() -

$newArr = array();
foreach ($your_array as $key => $val) {
  $newArr[$val['id']] = $val['name'];
}

print_r($newArr) // desired output
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
  • Although all answers are good, I'm going to accept this one as the correct answer. I had a foreach in mind but how to implement it confuses me always. I'll learn from this one (but from all other's as well. Everybody, have my upvote). – Mentos93 Jul 12 '16 at 10:08
3

You can do it using array_column and array_combine PHP function without applying custom logic.

Here is how you do it,

<?php
    $keys=array_column($mainarray,'id');
    $values=array_column($mainarray,'name');

    $finalarray=array_combine($keys,$values);

$finalarray will be your desired result.

array_combine creates an array by using one array for keys and another for its values.

array_column returns the values from a single column in the input array.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
3

I'd use array_combine which attaches new keys to values

$results = [
    ['id'=>1,'name'=>'John'],['id'=>2,'name'=>'Jane'],
];

$results = array_combine(
    array_column($results,'id'), //use 'id' column as keys
    array_column($results,'name') //use 'name' column as values
);
//now $results is [1=>'John', 2=>'Jane']
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165