PHP数组用键替换数字 [英] PHP array replace numbers with keys
本文介绍了PHP数组用键替换数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数组...
Array
(
[result] => Success
[finals] => Array
(
[0] => Array
(
[id] => 633
[name] => RESULT84
)
[0] => Array
(
[id] => 766
[name] => RESULT2
)
[0] => Array
(
[id] => 22
[name] => RESULT1
)
)
)
我正在提取这样的名字...
And I am extracting the names like this...
$names = array_column($data['finals'], 'name');
print_r($names);
哪个给我...
Array
(
[0] => RESULT84
[1] => RESULT2
[2] => RESULT1
)
我的问题是如何修改它,以便得到它...
My question is how can I modify it so that I get this...
Array
(
[RESULT84] => RESULT84
[RESULT2] => RESULT2
[RESULT1] => RESULT1
)
像array_fill_keys这样的东西是我最好的选择吗?
Is something like array_fill_keys my best bet?
推荐答案
将第三个参数name
传递到 array_column
作为密钥
Pass third parameter name
into array_column
to make it key
$names = array_column($data['finals'], 'name','name');
print_r($names);
这篇关于PHP数组用键替换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文