PHP合并和分组数组,并将它们汇总

时间:2019-06-20 06:48:54

标签: php codeigniter

我有以下数组,它们根据某些计算返回值:

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

上面的数组输出由不同的变量检索和保存。我想根据旅行计划合并/分组数组,同时总结所有类似旅行计划的溢价率。这样每个旅行计划只有一个阵列,每个旅行计划的溢价之和。

我该如何实现?输出应为以下内容:

    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
        )
    )

我尝试了以下功能,但将所有值加到一个:

        $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);

为我提供以下输出作为最终数组:

Array
(
    [premium_price] => 103453.855
    [travel_plan] => Senior
    [upper_limit] => 85
    [eligibility] => 76 to 85 Yrs
    [lower_limit] => 76
    [no_travellers] => 1
)

3 个答案:

答案 0 :(得分:0)

array_merge(array1,array2,....)将是您的答案。 这是一个例子。

$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);

输出将是这样

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
        )

)

答案 1 :(得分:0)

此逻辑将解决90%的问题,您所需要做的就是提取上限和下限。

$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;
}

数组的输出结果如下 https://3v4l.org/4X2ts#output

答案 2 :(得分:0)

您可以通过这种方式尝试。

@Mapper
public interface MyMapper{

// you probably don't need the return, right?
void updateEntityWithEntity(Entity sourceEntity, @MappingTarget Entity targetEntity);

// you need to implement the list merging yourself.. MapStruct has no clue what you intent here
default updateList( List< AnotherEntity > sourceList, @MappingTarget List< AnotherEntity> targetList ) {
   for ( int i=0; i<sourceList.size(); i++ ) {
        updateAnotherEntityWithAnotherEntity( sourceList.get( i ), targetList.get( i ) );
   }
}

// used by above code in 
@Mapping( target = "id", ignore = true )
void updateAnotherEntityWithAnotherEntity(AnotherEntity sourceEntity, @MappingTarget AnotherEntity targetEntity);


}