PHP foreach与数组嵌套


PHP foreach with array nesting

我有一个使用PHP CodeIgniter的Cart库的应用程序。我需要这个输出,将信息发送到google分析:

$itemsga = array(
array('sku'=>'SDFSDF', 'name'=>'Shoes', 'category'=>'Footwear', 'price'=>'100', 'quantity'=>'1'),
array('sku'=>'123DSW', 'name'=>'Sandles', 'category'=>'Footwear', 'price'=>'87', 'quantity'=>'1'),
array('sku'=>'UHDF93', 'name'=>'Socks', 'category'=>'Footwear', 'price'=>'5.99', 'quantity'=>'2')
);
为了实现这一点,我创建了以下代码:
$itemsga = array(
  foreach ($this->cart->contents() as $items){
    array(
    'sku'              => $items['id'],
    'name'             => $items['name'],
    'price'            => $items['price'],
    'quantity'         => $items['qty'],
    ),
  }//endforeach
);

由于某种原因,我的屏幕出现了白屏。没有错误显示,但我的数组没有被构建。

我知道这可能是一个愚蠢的问题,但是我被卡住了。有人能帮帮我吗?

干杯!

修改代码为:-

$itemsga = array(); // define array variable
  foreach ($this->cart->contents() as $items){
    $itemsga[] = array(
    'sku'              => $items['id'],
    'name'             => $items['name'],
    'price'            => $items['price'],
    'quantity'         => $items['qty'],
    ); // done indexing and add complete array to each index.
  }//endforeach