迭代多维数组中的问题

时间:2012-04-27 11:57:45

标签: php multidimensional-array iteration

Array
(
    [1] => Array
        (
            [0] => Array
                (

                    [title] => New Car 
                    [description] => hi i am 
                )

            [1] => Array
                (

                    [title] => Honda city for sale!
                    [description] => I want to sell my Honda city ( Make-1998) ( Model- 1.5 Exi). The car has run for 
around 90000 kms. It is silver in colour.
                )

            [2] => Array
                (

                    [title] => Maruti Alto
                    [description] => Maruti AltoMaruti AltoMaruti AltoMaruti AltoMaruti AltoMaruti Alto
                )

        )

    [6] => Array
        (
            [0] => Array
                (

                    [title] => LG 1.5 AC
                    [description] => LG 1.5 ACLG 1.5 ACLG 1.5 ACLG 1.5 ACLG 1.5 ACLG 1.5 ACLG 1.5 AC
                )

        )

)

我需要收集二维数组中的所有值,如下所示:

Array
(

          [0] => Array
                (

                    [title] => New Car 
                    [description] => hi i am 
                )

            [1] => Array
                (

                    [title] => Honda city for sale!
                    [description] => I want to sell my Honda city ( Make-1998) ( Model- 1.5 Exi). The car has run for 
around 90000 kms. It is silver in colour.
                )

            [2] => Array
                (

                    [title] => Maruti Alto
                    [description] => Maruti AltoMaruti AltoMaruti AltoMaruti AltoMaruti AltoMaruti Alto
                )


            [3] => Array
                (

                    [title] => LG 1.5 AC
                    [description] => LG 1.5 ACLG 1.5 ACLG 1.5 ACLG 1.5 ACLG 1.5 ACLG 1.5 ACLG 1.5 AC
                )

        )

)

谢谢..

1 个答案:

答案 0 :(得分:2)

这看起来非常像家庭作业...... 如果你肯定只有两个级别,那么使用两个嵌套的foreach:

$result = array();
foreach ($source as $subarray) {
foreach ($subarray as $item)
$result[] = $item;
}

如果你有更多关卡,你需要一个递归函数。