Reputation:
The array below is my current situation. Through a loop new data get's added. I've tried 'array_merge_recursive' as well as this this accepted answer. But it doesn't seem to work or I'm using it wrong.
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
)
)
)
)
)
)
)
Desired situation is 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
Upvotes: 4
Views: 83
Reputation: 10447
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;
}
Upvotes: 0
Reputation: 78994
Pretty sure this will work:
$result = call_user_func_array('array_merge_recursive', $array);
Upvotes: 4