Suganya Rajasekar Suganya Rajasekar - 7 months ago 34
PHP Question

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?

Answer

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:

Comments