I have the following arrays which return the values based on certain calculations :
print_r($adult_array);
print_r($children_array);
print_r($senior_array);
//Adult array start
Array
(
[0] => Array
(
[travel_plan] => Business
[premium_price] => 1336.81
[eligibility] => Up to 75 Yrs
[lower_limit] => 0
[upper_limit] => 75
[no_travellers] => 2
)
[1] => Array
(
[travel_plan] => Holiday
[premium_price] => 22960.81
[eligibility] => Up to 75 Yrs
[lower_limit] => 0
[upper_limit] => 75
[no_travellers] => 2
)
[2] => Array
(
[travel_plan] => Schengen
[premium_price] => 11740.81
[eligibility] => Up to 75 Yrs
[lower_limit] => 0
[upper_limit] => 75
[no_travellers] => 2
)
[3] => Array
(
[travel_plan] => Student
[premium_price] => 22960.81
[eligibility] => Up to 30 Yrs
[lower_limit] => 0
[upper_limit] => 30
[no_travellers] => 2
)
)
//Adult array end
//Children array start
Array
(
[0] => Array
(
[travel_plan] => Student
[premium_price] => 5740.205
[eligibility] => Up to 30 Yrs
[lower_limit] => 0
[upper_limit] => 30
[no_travellers] => 1
)
)
//Children array end
//Senior array start
Array
(
[0] => Array
(
[travel_plan] => Senior
[premium_price] => 38714.41
[eligibility] => 76 to 85 Yrs
[lower_limit] => 76
[upper_limit] => 85
[no_travellers] => 1
)
)
//Senior array end
The above array output is retrieved and held by different variables. I would like to merge/group the arrays depending on the travel plan and at the same time sum up the premium rates for all similar travel plans. So that there is only one array per travel plan and a sum of the premium price per travel plan.
How can I achieve this? The output should be the following :
Array(
[0]=>Array(
[travel_plan] => Business
[premium_price] => 1336.81
[eligibility] => Up to 75 Yrs
[lower_limit] => 0
[upper_limit] => 75
[no_travellers] => 2
)
[1]=>Array(
[travel_plan] => Holiday
[premium_price] => 22960.81
[eligibility] => Up to 75 Yrs
[lower_limit] => 0
[upper_limit] => 75
[no_travellers] => 2
)
[2]=>Array(
[travel_plan] => Schengen
[premium_price] => 11740.81
[eligibility] => Up to 75 Yrs
[lower_limit] => 0
[upper_limit] => 75
[no_travellers] => 2
)
[3]=>Array(
[travel_plan] => Student
[premium_price] => 28701.015
[eligibility] => Up to 30 Yrs
[lower_limit] => 0
[upper_limit] => 30
[no_travellers] => 2
)
[4]=>Array(
[travel_plan] => Senior
[premium_price] => 38714.41
[eligibility] => Up to 30 Yrs
[lower_limit] => 0
[upper_limit] => 30
[no_travellers] => 1
)
)
I have tried the following function but it adds up all the values to one :
$final_package = array();
foreach ($travel_plan as $key => $values) {
foreach ($values as $label => $count) {
// Create a node in the array to store the value
if (!array_key_exists($label, $final_package)) {
$final_package[$label] = 0;
}
// Add the value to the corresponding node
if(is_string($count)){
$final_package[$label] = $count;
}else{
$final_package[$label] += $count;
}
}
}
// Sort the array in descending order of values
arsort($final_package);
print_r($final_package);
Giving me the following output as the final array :
Array
(
[premium_price] => 103453.855
[travel_plan] => Senior
[upper_limit] => 85
[eligibility] => 76 to 85 Yrs
[lower_limit] => 76
[no_travellers] => 1
)