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
)
array_merge(array1,array2,....)
will be your answer. Here is an example.
$a = [
[
'travel_plan '=>'Schengen',
'premium_price '=>'Rs 1,200'
],
[
'travel_plan '=>'Business ',
'premium_price '=>'Rs 1,300'
]
];
$b = [
[
'travel_plan '=>'Senior ',
'premium_price '=>'Rs 1,600'
]
];
$c =array_merge ($a,$b);
echo '<pre>';
print_r($c);
The Output will be like this
Array
(
[0] => Array
(
[travel_plan ] => Schengen
[premium_price ] => Rs 1,200
)
[1] => Array
(
[travel_plan ] => Business
[premium_price ] => Rs 1,300
)
[2] => Array
(
[travel_plan ] => Senior
[premium_price ] => Rs 1,600
)
)
This logic will solve 90% percent of your problem all you need to do is to extract upper limit and lower limits.
$bigArray = array_merge($adult_array, $senior_array, $children_array);
$BusinessTravels = sortByTravelPlan('Business', $bigArray);
$StudentTravels = sortByTravelPlan('Student', $bigArray);
$SeniorTravels = sortByTravelPlan('Senior', $bigArray);
$SchengenTravels = sortByTravelPlan('Schengen', $bigArray);
print_r($BusinessTravels);
print_r($StudentTravels);
print_r($SeniorTravels);
$finalResult = array_merge($BusinessTravels, $StudentTravels, $SeniorTravels, $SchengenTravels );
function sortByTravelPlan($type, $bigArray){
$Array = [];
foreach ($bigArray as $key => $value) {
if($value['travel_plan'] == $type){
array_push($Array, $bigArray[$key]);
}
}
return processArray($Array);
}
function processArray($array){
$result = [];
$prev_price = null;
$max_age = null;
foreach ($array as $key => $value) {
$result['travel_plan'] = $value['travel_plan'];
if($value['premium_price'] > $prev_price){
$result['premium_price'] = $value['premium_price'];
}
}
$result['upper_limit'] = 30;
$result['lower_limit'] = 0;
$result['no_travellers'] = count($array);
return $result;
}
Output resulting from your array is below https://3v4l.org/4X2ts#output
You can try by this way.
$full_array = array_merge($adult_array,$children_array,$senior_array);
$result = [];
foreach($full_array as $item){
// Count how many index
if(isset($result[$item['travel_plan']])){
$result[$item['travel_plan']] += 1;
}else{
$result[$item['travel_plan']] = 1;
}
// Sum total value
if(isset($result[$item['travel_plan'].'_total'])){
$result[$item['travel_plan'].'_total'] = $result[$item['travel_plan'].'_total'] + $item['premium_price'];
}else{
$result[$item['travel_plan'].'_total'] = $item['premium_price'];
}
}