mayank
mayank

Reputation: 1

issue in iterating multidimensional array

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
                )

        )

)

I need to collect all value in a two dimensional array like this:

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
                )

        )

)

Thanks..

Upvotes: 0

Views: 69

Answers (1)

Gaet
Gaet

Reputation: 689

This indeed looks very much like homework... If you're sure to have only two levels, then use two nested foreach like this:

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

If you have more levels, you'll need a recursive function.

Upvotes: 2

Related Questions