gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:27:12 25 4
gpt4 key购买 nike

我从查询中得到了这个数组。

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

我需要将数组索引设置为数组值 (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
)
)

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

如何将数组转换为指定的索引值..?

最佳答案

这正是 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.

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

关于php - 如何将 php 数组索引设置为数组值...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44183847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com