1

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?

3 Answers 3

1

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
        )

)
Sign up to request clarification or add additional context in comments.

5 Comments

'@Dhaval Bharadva'what you mean 2 levels depth!!... I can't understand
@SuganyaRajasekar If your array contains one more dimentional level then call_user_func_array('array_merge', $a); will not merge that level.
you mean you specify the main array or sub (nested)arrays??
@SuganyaRajasekar please check my updated answer so you will get clear idea about use of RecursiveArrayIterator instead of array_merge.
@Dhaval BharadvaNow I get clear vision of it.. :) But My array never won't be like this.. :) :) Anyways...Thank you soo much for your Idea & answer
1

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

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.