2

I have this array from a query.

Array
(
    [0] => Array
        (
            [user_id] => 5
            [first_name] => Diyaa
            [profile_pic] => profile/user5.png
        )

    [1] => Array
        (
            [user_id] => 8
            [first_name] => Raj
            [profile_pic] => profile/user8.jpg
        )

    [2] => Array
        (
            [user_id] => 10
            [first_name] => Vanathi
            [profile_pic] => profile/user10.jpg
        )
)

I need to set array index as like array value (user_id) as given below:

Array
(
    [5] => Array
        (
            [user_id] => 5
            [first_name] => Diyaa
            [profile_pic] => profile/user5.png
        )

    [8] => Array
        (
            [user_id] => 8
            [first_name] => Raj
            [profile_pic] => profile/user8.jpg
        )

    [10] => Array
        (
            [user_id] => 10
            [first_name] => Vanathi
            [profile_pic] => profile/user10.jpg
        )
)

Note: user_id is an unique value, it won't repeat again. No need to worry about index value.

How to convert and get that array as specified index value..?

2
  • 1
    @ThisGuyHasTwoThumbs I think he wants what laravel calls keyBy Commented May 25, 2017 at 15:11
  • @apokryfos ah I see - removed comment :) Commented Jun 1, 2017 at 15:52

3 Answers 3

8

This is exactly what array_column() is for:

$result = array_column($array, null, 'user_id');

array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

column_key

The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).

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

1 Comment

using this pretty function daily, first time to try to pass the second parameter as null :)
6

You can try this code, here I do some extra work. Refer to AbraCadaver's clever answer $result = array_column($array, null, 'user_id');.

array_combine(array_column($array, 'user_id'), $array);

5 Comments

Clever. I always ended up using a loop. Nice combo!
Out of curiosity - I don't see it in the documentation - but is your understanding that array_column preserves order? That would be necessary for this to work.
@yes, I've tryed it. I think here php may be use the iterator to get it.
Why array_combine()? The third parameter to array_column() does just this.
@AbraCadaver great way. I've never notice it. Thank you.
0

Both structures are unnecessarily complex and redundant. Why not

$foo = array(5 =>
          array('first_name' => 'Diyaa',
                'profile_pic' => 'profile/user5.png'),
             8 =>
          array('first_name' => 'Raj',
                'profile_pic' => 'profile/user8.png'),
          ...
            );

Then access it via $foo[$user_id], which will give you a 2-element associative array such as

          array('first_name' => 'Raj',
                'profile_pic' => 'profile/user8.png'),

For changing a profile_pic:

$foo[$user_id]['profile_pic'] = $new_pic;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.