Saini
Saini

Reputation: 21

Convert a Multidimensional array to single Dimension

This is the array as I currently have,please convert it to the below array with single dimensional array:-

Array
(
    [0] => Array
        (
            [is_custom] => yes
        )

    [1] => Array
        (
            [custom_amount] => 45
        )

    [2] => Array
        (
            [custom_amount_text] => Enter Amount
        )

    [3] => Array
        (
            [amount_btn] => Dropdown
        )

    [4] => Array
        (
            [multiple_amounts] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
        )

    [5] => Array
        (
            [recurring_plans] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
        )

    [6] => Array
        (
            [recurring_interval] => WyJNb250aGx5IiwiUXVhcnRlcmx5IiwiSGFsZi1ZZWFybHkiXQ==
        )

    [7] => Array
        (
            [admin_mail_subject] => 
        )

    [8] => Array
        (
            [admin_mail_body] => 
        )

    [9] => Array
        (
            [user_mail_subject] => 
        )

    [10] => Array
        (
            [user_mail_body] => 
        )

    [11] => Array
        (
            [is_recurrance] => yes
        )

    [12] => Array
        (
            [is_onetime] => yes
        )

)

Now I want to convert this into something like this

Array([0]=> 
[is_custom] => yes 
[custom_amount] => 45 
[custom_amount_text] => Enter Amount 
[amount_btn] => Dropdown 
[multiple_amounts] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
[recurring_plans] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
)

Array 0 key will have all the keys can It be possible anyhow.I tried but failed. Please help me with a possible solution

Upvotes: 0

Views: 84

Answers (2)

Ramalingam Perumal
Ramalingam Perumal

Reputation: 1427

Use RecursiveIteratorIterator for fast and simple.

$output_array = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($input_array)), 0); //$input_array-Replace your arrray

echo "<pre>";
print_r($output_array);
echo "</pre>";

Result:

Array
(
    [0] => yes
    [1] => 45
    [2] => Enter Amount
    [3] => Dropdown
    [4] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
    [5] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
    [6] => WyJNb250aGx5IiwiUXVhcnRlcmx5IiwiSGFsZi1ZZWFybHkiXQ==
    [7] => 
    [8] => 
    [9] => 
    [10] => 
    [11] => yes
    [12] => yes
)

Run Yourself:

http://sandbox.onlinephpfunctions.com/code/86a50fd58455d64c4de074b888d5d2ea5ee1dd13

Upvotes: 2

Vini Dubey
Vini Dubey

Reputation: 71

try this one

$newArray=array_map(function($v){
return current($v);
},$oldArray);

Upvotes: 1

Related Questions