将多维数组转换为单数组


Turn multidimensional array into single array?

我正在尝试通过迭代购物车创建的数组创建多维数组:

Array (
    [array] => Array (
        [0] => Array (
            [product_id] => 7
            [prod_count] => 1
            [price] => 19.99
        )
        [1] => Array (
            [product_id] => 6
            [prod_count] => 3
            [price] => 19.99
        )
        [2] => Array (
            [product_id] => 5
            [prod_count] => 2
            [price] => 19.99
        )
        [3] => Array (
            [product_id] => 4
            [prod_count] => 4
            [price] => 14.99
        )
        [4] => Array (
            [product_id] => 3
            [prod_count] => 5
            [price] => 19.99
        )
    )
)

变成这样:

$items = array(
array('product_id'=> $val,  'price'=>$price, 'quantity'=>$prod_count),
array('product_id'=>$val, 'price'=>$price, 'quantity'=>$prod_count),
array('product_id'=>$val, 'price'=>$price, 'quantity'=>$prod_count)
);

这样我就可以把它提供给谷歌分析电子商务跟踪代码,并为数组中的每个产品回显下面的代码。

_gaq.push(['_addItem',
   '<?php $order_id; ?>',          // trans ID **I have this part figured out**
   '<?php $product_id; ?>',        // SKU/code - required
   '',                             // product name 
   '',                             // category or variation
   '<?php $price; ?>',             // unit price - required
   '<?php $prod_count; ?>'         // quantity - required
]);

我从SO尝试了一堆解决方案,但它们似乎都不适合我的购物车数组,老实说我被难住了。SO的好人们,请给我指出正确的方向。

编辑*这是我的代码,首先创建多维数组。

$products = array();
for($i=0; $i<sizeof($productsIds); $i++){       
    $product = explode(":", $productsIds[$i]);
    $products[$i]['info'] = $this->Products->getProductById($product[0]);
    $products[$i]['count'] = $product[1];
    $products[$i]['price_count'] = $product[1]*$products[$i]['info']['product_price'];
    $orderarray[$i] = array(
        'product_id' => $products[$i]['info']['product_id'], 
        'prod_count' => $products[$i]['count'], 
        'price' => $products[$i]['info']['product_price']) ;

也许我可以改变它来创建一个更适合我想要的结果的数组。只是不知道怎么做。

您想使用原始数组并将它们传递给gaq?:

// You could remove the need for [0] if you changed your original code
<?php foreach($yourOriginalArray[0] as $item) { ?>
  _gaq.push(['_addItem',
     '<?php $order_id; ?>',          // trans ID **I have this part figured out**
     '<?php echo $item['product_id']; ?>',        // SKU/code - required
     '',                             // product name 
     '',                             // category or variation
     '<?php echo $item['price']; ?>',             // unit price - required
     '<?php echo $item['quantity']; ?>'         // quantity - required
  ]);
<?php } ?>

其他数据,即产品名称和类别,必须像处理product_id、price和quantity那样填充到数组中。

终于成功了

$ga_itemdata = call_user_func_array('array_merge',$ga_itemdata);
 foreach ($ga_itemdata as $gavar[0] => $details) {
echo "
_gaq.push(['_addItem',
    $order_id,
    $details[product_id],
    $details[prod_name],
    $none,
    $details[price],
    $details[prod_count]
    ]);
";
    }