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

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


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

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


函数式编程
浏览 168回答 3
3回答

一只斗牛犬

array_merge(array1,array2,....)将是你的答案。这是一个例子。$a = [&nbsp; [&nbsp; &nbsp; &nbsp; 'travel_plan '=>'Schengen',&nbsp; &nbsp; &nbsp; 'premium_price '=>'Rs 1,200'&nbsp; &nbsp; ],&nbsp; [&nbsp; &nbsp; &nbsp; 'travel_plan '=>'Business ',&nbsp; &nbsp; &nbsp; 'premium_price '=>'Rs 1,300'&nbsp; &nbsp; ]];$b = [&nbsp; [&nbsp; &nbsp; &nbsp; 'travel_plan '=>'Senior ',&nbsp; &nbsp; &nbsp; 'premium_price '=>'Rs 1,600'&nbsp; &nbsp; ]];$c =array_merge ($a,$b);echo '<pre>';print_r($c);输出将是这样的Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [travel_plan ] => Schengen&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [premium_price ] => Rs 1,200&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [travel_plan ] => Business&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [premium_price ] => Rs 1,300&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [travel_plan ] => Senior&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [premium_price ] => Rs 1,600&nbsp; &nbsp; &nbsp; &nbsp; ))

慕斯王

你可以试试这个方法。$full_array = array_merge($adult_array,$children_array,$senior_array);$result = [];foreach($full_array as $item){&nbsp; &nbsp; // Count how many index&nbsp; &nbsp; if(isset($result[$item['travel_plan']])){&nbsp; &nbsp; &nbsp; &nbsp; $result[$item['travel_plan']] += 1;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; $result[$item['travel_plan']] = 1;&nbsp; &nbsp; }&nbsp; &nbsp; // Sum total value&nbsp; &nbsp; if(isset($result[$item['travel_plan'].'_total'])){&nbsp; &nbsp; &nbsp; &nbsp; $result[$item['travel_plan'].'_total'] =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$result[$item['travel_plan'].'_total'] + $item['premium_price'];&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; $result[$item['travel_plan'].'_total'] = $item['premium_price'];&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP