Reduce the dimensions of a multidimensional array

江枫思渺然 提交于 2020-01-05 07:25:29

问题


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

回答1:


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



回答2:


$arr = array(
   "withinArray" => array(
       "withinMoreArray" => array(
           "andEvenMoreArray" => array(
           )
       )
   )
);

$arr = current($arr);
// OR
$arr = $arr['withinArray'];



回答3:


<?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'];
?>


来源:https://stackoverflow.com/questions/16153100/reduce-the-dimensions-of-a-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!