将多维数组转换为单个数组并删除键
array (size=1551884)
0 =>
array (size=1)
'entity_id' => string '131813' (length=6)
1 =>
array (size=1)
'entity_id' => string '213808' (length=6)
2 =>
array (size=1)
'entity_id' => string '712885' (length=6)
is it possible to convert it to single array without the key 'entity_id' without a loop?
array
0 =>
131813
1 =>
213808
2 =>
712885
I have tried this one :
call_user_func_array('array_merge', $array)
but somehow is only returning 1 element
UPDATE:
here are the benchmark results from the given answers to this question:
php version > 5.6
array_column: 0.20802903175354
foreach: 0.46231913566589
array_map: 1.021989107132
php version > 7
array_column: 0.079965829849243
foreach: 0.15323305130005
array_map: 0.28970503807068
array(size = 1551884)
0 =>
array(size = 1)
'ental_id'=> 字符串'131813'(长度= 6)
1 =>
array(size = 1)
'ental_id'=> 字符串'213808'(长度= 6)
2 =>
array(size = 1)
'ental_id'=> 字符串'712885'(长度= 6)
code> pre>
是否可以将其转换为单个数组,而不使用没有循环的键'entity_id'? p> \ n
array
0 =>
131813
1 =>
213808
2 =>
712885
code> pre>
\ n 我试过这个: p>
call_user_func_array('array_merge',$ array)
code> pre>
但不知何故只返回1个元素 p>
更新: p>
这里是来自这个问题的给定答案的基准测试结果: p>
php版本> 5.6 p>
array_column:0.20802903175354
foreach:0.46231913566589
array_map:1.021989107132
code> pre>
php version> 7 p>
array_column:0.079965829849243
foreach:0.15323305130005
array_map:0.28970503807068
code> pre>
div>
You can do this very easily with array_map
like this:
$result = array_map(function($value) {
return $value['entity_id'];
}, $originalArray);
Working example: https://3v4l.org/JOEMI
Of course you could also do it with a foreach
loop:
$result = [];
foreach($originalArray AS $entity) {
$result[] = $entity['entity_id'];
}
Working example: https://3v4l.org/9J5XH
I prefer the first option personally.
Update: the accepted answer is clearly the best way. Do that! Leaving this here for comparison.