Php 将多维数组转换为单个数组?
我试图将通过迭代购物车创建的数组创建的多维数组转换为:Php 将多维数组转换为单个数组?,php,zend-framework,multidimensional-array,google-analytics,Php,Zend Framework,Multidimensional Array,Google Analytics,我试图将通过迭代购物车创建的数组创建的多维数组转换为: Array ( [array] => Array ( [0] => Array ( [product_id] => 7 [prod_count] => 1 [price] => 19.99 ) [1] => Array ( [product_id] =&g
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)
);
因此,我可以将其输入到google analytics e-commmerce跟踪代码中,并为阵列中的每个产品回显下面的代码
_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
]);
\u gaq.push([''u addItem',
'',//trans ID**我已经找到了这个部分**
'',//SKU/代码-必需
'',//产品名称
'',//类别或变体
'',//单价-必需
''//数量-需要
]);
我从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']) ;
$products=array();
对于($i=0;$IPProducts->getProductById($product[0]);
$products[$i]['count']=$products[1];
$products[$i]['price_count']=$product[1]*$products[$i]['info']['product_price'];
$orderarray[$i]=数组(
'product_id'=>$products[$i]['info']['product_id'],
“产品计数”=>$products[$i]['count'],
“价格”=>$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 } ?>
//如果更改了原始代码,则可以不需要[0]
_gaq.push(附加项),
'',//trans ID**我已经找到了这个部分**
'',//SKU/代码-必需
'',//产品名称
'',//类别或变体
'',//单价-必需
''//数量-需要
]);
对于其他数据、产品名称和类别,您必须像处理产品id、价格和数量一样填充到数组中。最后,这一点起到了作用
$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]
]);
";
}
你想让我们凭空创建数据吗?:)我不知道你的意思。如果有什么我需要添加的,请告诉我,我会将其添加到问题中。例如,
123DSW
、Sandles
和Shoots
值来自哪里?这是谷歌分析的一个示例数组。我对它进行了修改,使之更符合我的情况。很抱歉给你带来了困惑。如果我甚至可以去掉数组的第一个维度,我想我可以让它工作起来,我遇到的问题是数组有很多维度。所以你想展平你的数组,使每个产品只有一个维度?