Turn multidimensional array into single array?

2019-08-23 02:32发布

问题:

I'm trying to turn a multidimensional array created by iterating over an array created by a shopping cart:

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
        )
    )
)

into something like this:

$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)
);

So that I can feed it to the google analytics e-commmerce tracking code and echo the code below for each product in the array.

_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
]);

Ive tried a bunch of solutions from SO but none of them seem to work right for my shopping cart array and honestly I'm stumped. Good people of SO please point me in the right direction.

EDIT* Here is my code that creates the multidimensional array in the first place.

$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']) ;

Maybe I could change this to create an array better suited for my desired outcome. Just not sure how.

回答1:

You want to use your original array and pass them to 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 } ?>

The other data, product name and category, you will have to populate into your array as you have done with product_id, price, and quantity.



回答2:

Finally this worked

$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]
    ]);
";
    }