Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Saturnlai's avatar

How to merge multiple array

I have 3 arrays

  1. The first
[
58814 => array:4 [
    "2022-09-16" => array:5 [
      "forecast_date" => "2022-09-16"
      "venue_id" => "58814"
      "statistics_group_code" => "58814"
      "forecast_last_year_actual" => 36288000
      "forecast_this_year_plan" => 28000000
    ]
    "Total" => array:5 [
      "forecast_date" => "Total"
      "venue_id" => "58814"
      "statistics_group_code" => "58814"
      "forecast_last_year_actual" => 105924000
      "forecast_this_year_plan" => 100000000
    ]
  ]
]

The second

[
58814 => array:4 [
    "2022-09-16" => array:10 [
      "order_date" => "2022-09-16"
      "event_name" => "Kansai"
      "statistics_group_code" => "58814"
      "total_revenue_virtual_catalog" => 0
      "total_revenue_sales_act_ttl" => 58530700
      "units_total_of_trunk_show" => 505
      "orders_total_of_trunk_show" => 194
      "total_revenue_event_venue" => 42712000
      "total_revenue_online_selling" => 4003000
      "total_revenue_other" => 11815700
    ]
	"Total" => array:10 [
      "order_date" => "Total"
      "event_name" => "Kansai"
      "statistics_group_code" => "58814"
      "total_revenue_virtual_catalog" => 0
      "total_revenue_sales_act_ttl" => 130658600
      "units_total_of_trunk_show" => 1284
      "orders_total_of_trunk_show" => 453
      "total_revenue_event_venue" => 100703000
      "total_revenue_online_selling" => 13066000
      "total_revenue_other" => 16889600
    ]
]				

The third

[
    58814 => array:4 [
    "2022-09-16" => array:6 [
      "appointment_date" => "2022-09-16"
      "total_forecast" => 35489000
      "total_appointments" => 170
      "total_arrived_appointments" => 143
      "total_arrived_attendance" => 205
      "total_number_of_attendees" => 239
    ]
    "Total" => array:6 [
      "appointment_date" => "Total"
      "total_forecast" => 85639000.0
      "total_appointments" => 455
      "total_arrived_appointments" => 402
      "total_arrived_attendance" => 701
      "total_number_of_attendees" => 778
    ]
]

Output

[
58814 => array:4 [
    "2022-09-16" => array:5 [
      "order_date" => "2022-09-16"
      "event_name" => "Kansai"
      "statistics_group_code" => "58814"
      "total_revenue_virtual_catalog" => 0
      "total_revenue_sales_act_ttl" => 58530700
      "units_total_of_trunk_show" => 505
      "orders_total_of_trunk_show" => 194
      "total_revenue_event_venue" => 42712000
      "total_revenue_online_selling" => 4003000
      "total_revenue_other" => 11815700
      "forecast_date" => "2022-09-16"
      "venue_id" => "58814"
      "statistics_group_code" => "58814"
      "forecast_last_year_actual" => 36288000
      "forecast_this_year_plan" => 28000000
    ]
    "Total" => array:5 [
      "order_date" => "Total"
      "event_name" => "Kansai"
      "statistics_group_code" => "58814"
      "total_revenue_virtual_catalog" => 0
      "total_revenue_sales_act_ttl" => 130658600
      "units_total_of_trunk_show" => 1284
      "orders_total_of_trunk_show" => 453
      "total_revenue_event_venue" => 100703000
      "total_revenue_online_selling" => 13066000
      "total_revenue_other" => 16889600
      "forecast_date" => "Total"
      "venue_id" => "58814"
      "statistics_group_code" => "58814"
      "forecast_last_year_actual" => 105924000
      "forecast_this_year_plan" => 100000000
    ]
  ]
]

I would like to merge 3 arrays to push all the objects from the other array to become an array by time. Please help me. Thank guys

0 likes
6 replies
Saturnlai's avatar

@vincent15000

It is not working with array_merge. Above that's what I want. Take your time if you can help me. Thanks so much

[
58814 => array:4 [
    "2022-09-16" => array:5 [
      "order_date" => "2022-09-16"
      "event_name" => "Kansai"
      "statistics_group_code" => "58814"
      "total_revenue_virtual_catalog" => 0
      "total_revenue_sales_act_ttl" => 58530700
      "units_total_of_trunk_show" => 505
      "orders_total_of_trunk_show" => 194
      "total_revenue_event_venue" => 42712000
      "total_revenue_online_selling" => 4003000
      "total_revenue_other" => 11815700
      "forecast_date" => "2022-09-16"
      "venue_id" => "58814"
      "statistics_group_code" => "58814"
      "forecast_last_year_actual" => 36288000
      "forecast_this_year_plan" => 28000000
    ]
    "Total" => array:5 [
      "order_date" => "Total"
      "event_name" => "Kansai"
      "statistics_group_code" => "58814"
      "total_revenue_virtual_catalog" => 0
      "total_revenue_sales_act_ttl" => 130658600
      "units_total_of_trunk_show" => 1284
      "orders_total_of_trunk_show" => 453
      "total_revenue_event_venue" => 100703000
      "total_revenue_online_selling" => 13066000
      "total_revenue_other" => 16889600
      "forecast_date" => "Total"
      "venue_id" => "58814"
      "statistics_group_code" => "58814"
      "forecast_last_year_actual" => 105924000
      "forecast_this_year_plan" => 100000000
    ]
  ]
]

Sinnbeck's avatar

What does "an array by time" mean?

Can you show an example of the expected output?

1 like
Saturnlai's avatar

@Sinnbeck thank you. Here's the output I prefer, please help me if possible

[
58814 => array:4 [
    "2022-09-16" => array:5 [
      "order_date" => "2022-09-16"
      "event_name" => "Kansai"
      "statistics_group_code" => "58814"
      "total_revenue_virtual_catalog" => 0
      "total_revenue_sales_act_ttl" => 58530700
      "units_total_of_trunk_show" => 505
      "orders_total_of_trunk_show" => 194
      "total_revenue_event_venue" => 42712000
      "total_revenue_online_selling" => 4003000
      "total_revenue_other" => 11815700
      "forecast_date" => "2022-09-16"
      "venue_id" => "58814"
      "statistics_group_code" => "58814"
      "forecast_last_year_actual" => 36288000
      "forecast_this_year_plan" => 28000000
    ]
    "Total" => array:5 [
      "order_date" => "Total"
      "event_name" => "Kansai"
      "statistics_group_code" => "58814"
      "total_revenue_virtual_catalog" => 0
      "total_revenue_sales_act_ttl" => 130658600
      "units_total_of_trunk_show" => 1284
      "orders_total_of_trunk_show" => 453
      "total_revenue_event_venue" => 100703000
      "total_revenue_online_selling" => 13066000
      "total_revenue_other" => 16889600
      "forecast_date" => "Total"
      "venue_id" => "58814"
      "statistics_group_code" => "58814"
      "forecast_last_year_actual" => 105924000
      "forecast_this_year_plan" => 100000000
    ]
  ]
]
SilenceBringer's avatar

@saturnlai assuming arrays have names $first, $second and $third

foreach ($first as $id => $data) {
	foreach ($data as $date => $values) {
		if (array_key_exists($id, $second) && array_key_exists($date, $second[$id])) {
			$first[$id][$date] = array_merge($first[$id][$date], $second[$id][$date]);
		}

		if (array_key_exists($id, $third) && array_key_exists($date, $third[$id])) {
			$first[$id][$date] = array_merge($first[$id][$date], $third[$id][$date]);
		}

		// optionally you can filter the values here
		$first[$id][$date] = Arr::only($first[$id][$date], [
			// ...
		]);
	}
}
1 like

Please or to participate in this conversation.