将多维数组转换为单个维度

时间:2016-11-01 06:00:54

标签: php arrays multidimensional-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
        )

)

现在我想把它转换成像这样的东西

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

数组0键将拥有所有键可以无论如何。我试过但失败了。 请帮我解决一个可能的解决方案

2 个答案:

答案 0 :(得分:2)

使用RecursiveIteratorIterator快速简单。

$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>";

<强>结果:

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

自己动手:

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

答案 1 :(得分:1)

尝试这个

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