减小多维数组的尺寸

时间:2013-04-22 17:28:04

标签: php multidimensional-array foreach

我无法通过这一系列产品信息进行迭代,并获得了为每个项目回显Google电子商务跟踪代码的预期结果。如何将尺寸减小一个。简而言之 -

如何转此:

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

进入这个:

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

3 个答案:

答案 0 :(得分:5)

这个例子的明显答案是:

$array = $array['array'];

但是,假设有多个数组作为第一级:

$array = call_user_func_array('array_merge',$array);

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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