简体   繁体   中英

Reduce the dimensions of a multidimensional array

Im having trouble iterating thru this array of product information with the desired result of echoing google ecommerce tracking code for each item. How do I reduce the dimension by one. In short -

How do turn this:

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 this:

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

The obvious answer for the example would be:

$array = $array['array'];

However, assuming there are multiple arrays as level one:

$array = call_user_func_array('array_merge',$array);
$arr = array(
   "withinArray" => array(
       "withinMoreArray" => array(
           "andEvenMoreArray" => array(
           )
       )
   )
);

$arr = current($arr);
// OR
$arr = $arr['withinArray'];
<?php
// Let's say this is your big 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
        )
    )
 )*/

$littlearray = $bigarray['array'];
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM