如何使其更简单(意味着我不希望使用多维数组)我只想一次只访问一行。将此多维3级深度数组转换为2级深度或1级深度数组,以便可以在PHP中轻松访问
Array
(
[0] => Array
(
[0] => Array
(
[id] => 2
[name] => 22" 120g Honey Comb
[sku] => Sonia-22
[price] => 100
[qty] => 74
[image] =>
[description] =>
[attribute_value_id] => null
[brand_id] => null
[category_id] => null
[store_id] => 3
[availability] => 1
)
[1] => Array
(
[id] => 4
[name] => Side Fringe2
[sku] => FrSideHH#1B-WU
[price] => 150
[qty] => 195
[image] =>
[description] =>
[attribute_value_id] => null
[brand_id] => ["1"]
[category_id] => ["5"]
[store_id] => 3
[availability] => 1
)
)
)
答案 0 :(得分:0)
您的varDump通过它的外观显示了一个有很多孩子的父数组,因此解决它的最简单方法如下:
foreach($mainArray[0] as $child){
foreach($child as $row){
echo $row['name'] . "\n";
}
}
如果要将名称用作单独的数组以供其他地方使用,请执行以下操作:
array_push($myNamesArray, $row['name']);
将其放在上面的foreach子循环中。
这为您提供了一个一维数组,您可以在代码的其他地方循环。