This question already has an answer here:
这个问题在这里已有答案:
- First value as the key in PHP array 2 answers
第一个值作为PHP数组2中的关键答案
I have an array like below;
我有一个像下面的数组;
Array
(
[0] => Array
(
[id] => 20
[contract_id] => 71
[service_type_id] => 184
[duration] => 33
[isUnlimited] => 0
)
[1] => Array
(
[id] => 21
[contract_id] => 71
[service_type_id] => 182
[duration] => 33
[isUnlimited] => 0
)
[2] => Array
(
[id] => 22
[contract_id] => 71
[service_type_id] => 183
[duration] => 33
[isUnlimited] => 0
)
)
Could you please help me to replace ['service_type_id'] value with its own index? Output will be as follows;
你能帮我用自己的索引替换['service_type_id']值吗?产量如下;
[184] => Array
(
[id] => 20
[contract_id] => 71
[service_type_id] => 184
[duration] => 33
[isUnlimited] => 0
)
I know, it is somewhere in *, I have seen it several months ago, but now I really really couldn't find it. If someone remembers and edit this question, I will for sure remove it, no problem.
我知道,它在*中的某个地方,几个月前我已经看过了,但现在我真的找不到它。如果有人记得并编辑了这个问题,我肯定会删除它,没问题。
Thanks in advance
提前致谢
1 个解决方案
#1
3
You can use array_column()
(http://php.net/manual/en/function.array-column.php) to do this...
您可以使用array_column()(http://php.net/manual/en/function.array-column.php)来执行此操作...
$arr = array_column ( $arr, null, 'service_type_id');
This creates a new array using service_type_id
as the new index.
这将使用service_type_id作为新索引创建一个新数组。
#1
3
You can use array_column()
(http://php.net/manual/en/function.array-column.php) to do this...
您可以使用array_column()(http://php.net/manual/en/function.array-column.php)来执行此操作...
$arr = array_column ( $arr, null, 'service_type_id');
This creates a new array using service_type_id
as the new index.
这将使用service_type_id作为新索引创建一个新数组。