dongrang2140 2019-06-20 06:48
浏览 35

PHP合并和分组数组并总结它们

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
)
  • 写回答

3条回答 默认 最新

  • doutao5419 2019-06-20 08:18
    关注

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改