Concatenate array with multiple keys (php)

The array below is my current situation. New data is added through the loop. I've tried 'array_merge_recursive' as well as this this accepted answer . But it doesn't work or I am using it incorrectly.

Array (
    [0] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2016] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 54
                                                    [startDate] => 01-01-2016
                                                    [endDate] => 31-12-2016
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2018] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 56
                                                    [startDate] => 01-01-2018
                                                    [endDate] => 31-12-2018
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
    [2] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2019] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 57
                                                    [startDate] => 01-01-2019
                                                    [endDate] => 31-12-2019
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
)

      

The desired situation looks something like this:

Array (
                [Customer] => Array (
                    [Weekend] => Array (
                        [2016] => Array (
                            [01] => Array (
                                [0] => Array (
                                    [id] => 54
                                    [startDate] => 01-01-2016
                                    [endDate] => 31-12-2016
                                    [price] => 0
                                )
                            )
                        )
                        [2018] => Array (
                            [01] => Array (
                                [0] => Array (
                                    [id] => 56
                                    [startDate] => 01-01-2018
                                    [endDate] => 31-12-2018
                                    [price] => 0
                                )
                            )
                        )
                        [2019] => Array (
                            [01] => Array (
                                [0] => Array (
                                    [id] => 57
                                    [startDate] => 01-01-2019
                                    [endDate] => 31-12-2019
                                    [price] => 0
                                )
                            )
                        )
                    )
                )
            )

      

If any other information is required, please ask! new here

+3


source to share


2 answers


Pretty sure this will work:



$result = call_user_func_array('array_merge_recursive', $array);

      

+5


source


This should do what you want:



$new_arr = [];
$arr = // Your current array;

foreach ($arr as $customer)
{
    foreach ($customer['Weekend'] as $year => $z)
    {
        foreach($z as $month => $y)
        {
            foreach($y as $entry)
            {
                Store($year, $month, $entry, $new_arr);
            }
        }
    }

}

function Store($year, $month, $entry, & $new_arr)
{
    if ( ! isset($new_arr['customer'][$year]))
    {
        $new_arr['customer'][$year] = array();
    }
    if ( ! isset($new_arr['customer'][$year][$month]))
    {
        $new_arr['customer'][$year][$month] = array();
    }
    $new_arr['customer'][$year][$month][] = $entry;
}

      

+1


source







All Articles