Suganya Rajasekar
Suganya Rajasekar

Reputation: 684

How to merge all the nested array values having same keys

I'm having an array like this,

Array
(
    [0] => Array
        (
            [0] => m_res1
            [1] => m_res2
        )

    [1] => Array
        (
            [0] => images
            [1] => pcgc_desc
            [2] => The_US_GRANT_A_Luxury_Collection_Hotel_usn_2
        )

    [2] => Array
        (
            [0] => Australis
        )

    [3] => Array
        (
            [0] => Greece
            [1] => download
        )

    [4] => Array
        (
            [0] => Studio
        )

)

I would like to get like this..

Array
(
    [0] => m_res1
    [1] => m_res2
    [2] => images
    [3] => pcgc_desc
    [4] => The_US_GRANT_A_Luxury_Collection_Hotel_usn_2
    [5] => Australis
    [6] => Greece
    [7] => download
    [8] => Studio
)

i.e:, merge the nested array values and make it as an one array..

Is this possible?

Upvotes: 1

Views: 296

Answers (3)

Dhaval Bharadva
Dhaval Bharadva

Reputation: 3083

You can use the Standard PHP Library (SPL) In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:

$finalArray = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($a)); //$a is your multidimentional original array
foreach($iterator as $v) {
    $finalArray[] = $v;
}
echo "<pre>";
print_r($finalArray);  //print your array
echo "</pre>";

output:

Array
(
    [0] => m_res1
    [1] => m_res2
    [2] => images
    [3] => pcgc_desc
    [4] => The_US_GRANT_A_Luxury_Collection_Hotel_usn_2
    [5] => Australis
    [6] => Greece
    [7] => download
    [8] => Studio
)  

Note: Alternatively you can use call_user_func_array('array_merge', $a); to flatten your array but it will only work with 2 levels depth.

Explaination:

$a = Array(
    Array("m_res1","m_res2"),
    Array("images","pcgc_desc","The_US_GRANT_A_Luxury_Collection_Hotel_usn_2"),
    Array("Australis"),
    Array("Greece","download"),
    Array("Studio"),
    Array(
        Array("3rd Level")
        )
    );

$finalArray = call_user_func_array('array_merge', $a);

echo "<pre>";
print_r($finalArray);  //print your array
echo "</pre>";

output:

Array
(
    [0] => m_res1
    [1] => m_res2
    [2] => images
    [3] => pcgc_desc
    [4] => The_US_GRANT_A_Luxury_Collection_Hotel_usn_2
    [5] => Australis
    [6] => Greece
    [7] => download
    [8] => Studio
    [9] => Array
        (
            [0] => 3rd Level
        )

)

Upvotes: 1

Jatin Raikwar
Jatin Raikwar

Reputation: 394

Use like this

$main = array();
foreach($arr as $key => $arrsub)
{
    foreach($arrsub as $key => $value)
    {
        array_push($main,$value);
    }
}
echo "<pre>";
print_r($main);
echo "</pre>";

for more information regarding array_push

http://php.net/manual/en/function.array-push.php

Upvotes: 1

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

You can do something like this to convert the associative array to a normal array,

// Here $arr is your original array
$result_array = call_user_func_array('array_merge', $arr);

// display $result_array
echo "<pre>";
print_r($result_array);
echo "</pre>";

Output:

Array
(
    [0] => m_res1
    [1] => m_res2
    [2] => images
    [3] => pcgc_desc
    [4] => The_US_GRANT_A_Luxury_Collection_Hotel_usn_2
    [5] => Australis
    [6] => Greece
    [7] => download
    [8] => Studio
)

Here are the relevant references:

Upvotes: 1

Related Questions