我正在尝试通过迭代购物车创建的数组来创建一个多维数组:
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的解决方案,但它们似乎都不适合我的购物车阵列,说实话,我很难过。好的人,请指出我正确的方向。
编辑*这是我的代码,它首先创建了多维数组。
$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']) ;
也许我可以改变它来创建一个更适合我想要的结果的数组。只是不确定如何。
答案 0 :(得分:1)
您想使用原始数组并将其传递给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一样填充到数组中。
答案 1 :(得分:1)
最后这个工作
$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]
]);
";
}