如何将php数组索引设置为数组值...?

时间:2022-06-21 22:29:37

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:

我需要将数组索引设置为数组值(user_id),如下所示:

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.

注意:user_id是唯一值,不会再重复。无需担心索引值。

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

如何转换并获取指定索引值的数组..?

3 个解决方案

#1


4  

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

你可以试试这段代码,在这里我做一些额外的工作。请参阅AbraCadaver的巧妙答案$ result = array_column($ array,null,'user_id');.

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

#2


4  

This is exactly what array_column() is for:

这正是array_column()的用途:

$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.

array_column()返回输入的单个列中的值,由column_key标识。可选地,可以提供index_key以通过来自输入数组的index_key列的值来索引返回的数组中的值。

column_key

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).

要返回的值列。此值可以是要检索的列的整数键,也可以是关联数组或属性名称的字符串键名。返回完整的数组或对象也可能为NULL(这与index_key一起用于重新索引数组)。

#3


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

然后通过$ foo [$ user_id]访问它,它将为你提供一个2元素的关联数组,如

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

For changing a profile_pic:

要更改profile_pic:

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

#1


4  

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

你可以试试这段代码,在这里我做一些额外的工作。请参阅AbraCadaver的巧妙答案$ result = array_column($ array,null,'user_id');.

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

#2


4  

This is exactly what array_column() is for:

这正是array_column()的用途:

$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.

array_column()返回输入的单个列中的值,由column_key标识。可选地,可以提供index_key以通过来自输入数组的index_key列的值来索引返回的数组中的值。

column_key

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).

要返回的值列。此值可以是要检索的列的整数键,也可以是关联数组或属性名称的字符串键名。返回完整的数组或对象也可能为NULL(这与index_key一起用于重新索引数组)。

#3


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

然后通过$ foo [$ user_id]访问它,它将为你提供一个2元素的关联数组,如

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

For changing a profile_pic:

要更改profile_pic:

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