Выровнять многомерный массив - PHP - PullRequest
0 голосов
/ 22 февраля 2020

Мне нужно сгладить следующий многомерный массив

 Array
    (
        [0] => Array
            (
                [shipping] => Array
                    (
                        [shipping_address_nickname] => CHILD SHIPPING - MAIN
                        [shipping_first_name] => Harshana
                        [shipping_last_name] => Child Shipping
                        [shipping_company] => Home
                        [shipping_country] => LK
                        [shipping_address_1] => 236, Maimbula
                        [shipping_postcode] => 11880
                        [shipping_city] => Nittambuwa
                        [shipping_state] => 
                    )

            )

        [1] => Array
            (
                [shipping2] => Array
                    (
                        [shipping2_address_nickname] => CHILD - 1
                        [shipping2_first_name] => Child
                        [shipping2_last_name] => 1
                        [shipping2_company] => Child - 1
                        [shipping2_country] => LK
                        [shipping2_address_1] => 236, Maimbula
                        [shipping2_postcode] => 11880
                        [shipping2_city] => Nittambuwa
                        [shipping2_state] => 
                    )

            )

        [2] => Array
            (
                [shipping3] => Array
                    (
                        [shipping3_address_nickname] => 
                        [shipping3_first_name] => 
                        [shipping3_last_name] => 
                        [shipping3_company] => 
                        [shipping3_country] => 
                        [shipping3_address_1] => 
                        [shipping3_postcode] => 
                        [shipping3_city] => 
                        [shipping3_state] => 
                    )

            )

        [3] => Array
            (
                [shipping4] => Array
                    (
                        [shipping4_address_nickname] => 
                        [shipping4_first_name] => 
                        [shipping4_last_name] => 
                        [shipping4_company] => 
                        [shipping4_country] => 
                        [shipping4_address_1] => 
                        [shipping4_postcode] => 
                        [shipping4_city] => 
                        [shipping4_state] => 
                    )

            )

        [4] => Array
            (
                [shipping] => Array
                    (
                        [shipping_address_nickname] => HARSHANA
                        [shipping_first_name] => Harshana
                        [shipping_last_name] => Child Shipping
                        [shipping_company] => Home
                        [shipping_country] => LK
                        [shipping_address_1] => 236, Maimbula
                        [shipping_postcode] => 11880
                        [shipping_city] => Nittambuwa
                        [shipping_state] => 
                    )

            )

        [5] => Array
            (
                [shipping2] => Array
                    (
                        [shipping2_address_nickname] => 
                        [shipping2_first_name] => 
                        [shipping2_last_name] => 
                        [shipping2_company] => 
                        [shipping2_country] => 
                        [shipping2_address_1] => 
                        [shipping2_postcode] => 
                        [shipping2_city] => 
                        [shipping2_state] => 
                    )

            )

        [6] => Array
            (
                [shipping3] => Array
                    (
                        [shipping3_address_nickname] => PARENT 1
                        [shipping3_first_name] => Parent
                        [shipping3_last_name] => 1
                        [shipping3_company] => Parent - 1
                        [shipping3_country] => LK
                        [shipping3_address_1] => 236, Maimbula
                        [shipping3_postcode] => 11880
                        [shipping3_city] => Nittambuwa
                        [shipping3_state] => 
                    )

            )

        [7] => Array
            (
                [shipping4] => Array
                    (
                        [shipping4_address_nickname] => PARENT 2
                        [shipping4_first_name] => Parent
                        [shipping4_last_name] => 2
                        [shipping4_company] => Parent - 2
                        [shipping4_country] => LK
                        [shipping4_address_1] => 236, Maimbula
                        [shipping4_postcode] => 11880
                        [shipping4_city] => Nittambuwa
                        [shipping4_state] => 
                    )

            )

    )

должно быть так:

Array
(
    [shipping] => Array
        (
            [shipping_address_nickname] => HARSHANA
            [shipping_first_name] => Harshana
            [shipping_last_name] => Child Shipping
            [shipping_company] => Home
            [shipping_country] => LK
            [shipping_address_1] => 236, Maimbula
            [shipping_postcode] => 11880
            [shipping_city] => Nittambuwa
            [shipping_state] => 
        )

    [shipping2] => Array
        (
            [shipping2_address_nickname] => 
            [shipping2_first_name] => 
            [shipping2_last_name] => 
            [shipping2_company] => 
            [shipping2_country] => 
            [shipping2_address_1] => 
            [shipping2_postcode] => 
            [shipping2_city] => 
            [shipping2_state] => 
        )

    [shipping3] => Array
        (
            [shipping3_address_nickname] => PARENT 1
            [shipping3_first_name] => Parent
            [shipping3_last_name] => 1
            [shipping3_company] => Parent - 1
            [shipping3_country] => LK
            [shipping3_address_1] => 236, Maimbula
            [shipping3_postcode] => 11880
            [shipping3_city] => Nittambuwa
            [shipping3_state] => 
        )

    [shipping4] => Array
        (
            [shipping4_address_nickname] => PARENT 2
            [shipping4_first_name] => Parent
            [shipping4_last_name] => 2
            [shipping4_company] => Parent - 2
            [shipping4_country] => LK
            [shipping4_address_1] => 236, Maimbula
            [shipping4_postcode] => 11880
            [shipping4_city] => Nittambuwa
            [shipping4_state] => 
        )
)

Я использую php.

1 Ответ

0 голосов
/ 22 февраля 2020
$flattenedArray = [];

foreach ($mainArray as $subArray) {
    foreach ($subArray as $shippingKey => $subSubArray)
        $flattenedArray[$shippingKey] = $subSubArray;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...