多维数组不能预先

时间:2017-07-06 06:09:59

标签: php arrays multidimensional-array

我有这个数组:

array:2 [▼
  0 => array:6 [▼
    "id" => 1
    "name" => "Longline T-Shirt In Fake Linen With All Over Floral Print"
    "price" => 18.11
    "quantity" => 1
    "attributes" => []
    "conditions" => []
  ]
  1 => array:6 [▼
    "id" => 3
    "name" => "Longline T-Shirt With All Over Aztec Print"
    "price" => 16.99
    "quantity" => 1
    "attributes" => []
    "conditions" => []
  ]
]

(来自laravel的dd)

但是当我在数组($ all_order)上运行foreach时,它只返回第一个细节。

尝试使用以下方法展开数组:

$result = [];
        array_walk_recursive($all_order, function($v) use (&$result) {
            $result[] = $v;
        });
        dd( $result);

结果:

 0 => 1
  1 => "Longline T-Shirt In Fake Linen With All Over Floral Print"
  2 => 18.11
  3 => 1
  4 => 3
  5 => "Longline T-Shirt With All Over Aztec Print"
  6 => 16.99
  7 => 1

问题是,我只需要id +数量来降低产品表中库存的价值....我堆叠了大约4个小时-_-“

1 个答案:

答案 0 :(得分:3)

我猜你的原始foreach代码中有错误。这就是你通常的做法:

foreach ($all_order as $value) {
  $id = $value['id'];
  $quantity = $value['quantity'];
  // then do something with your values...
}