从特定键的嵌套数组行收集所有值 [英] Collect all values from nested array row of specific key
本文介绍了从特定键的嵌套数组行收集所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要创建一个由存储在特定键(Product_Id)的数组行中的所有值组成的列表。目前,对我的$BESTELLERS变量执行print_r会产生以下数组:
Array
(
[0] => stdClass Object
(
[product_id] => 178
[order_item_qty] => 9
)
[1] => stdClass Object
(
[product_id] => 233
[order_item_qty] => 4
)
[2] => stdClass Object
(
[product_id] => 179
[order_item_qty] => 1
)
)
其他SO答案引导我尝试:
$ids = array_column($bestsellers, 'product_id');
.但这产生了一个空数组,我猜是因为我试图获取的行嵌套在那里?考虑到这一点,我试着
foreach($bestsellers as $bestseller) {
$ids = array_column($bestsellers, 'product_id');
}
.这根本没有产生任何结果。 希望有人能帮我指出我哪里出了问题。谢谢!
推荐答案
嵌套值是对象,而不是数组(您在输出中看不到stdClass Object
吗?)array_column
用于二维数组。您需要使用对象语法访问属性。
$ids = array_map(function($x) { return $x->product_id; }, $bestsellers);
这篇关于从特定键的嵌套数组行收集所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文