drny60365
2016-06-06 10:04
浏览 46

获取关键php数组具有相同值的值的总和

I have below array,

Array ( [0] => Array ( [report_id] => 1 [amount] => 100.00 [category_name] => Trial1 ) [1] => Array ( [report_id] => 1 [amount] => 150.00 [category_name] => Trial2 ) [2] => Array ( [report_id] => 1 [amount] => 200.00 [category_name] => Trial2 ) 

What i want to send to have JSON with below format

It will get some of Equal category name and then send it as json.

[{'category_name': 'Trial1', 'Sum':100]}, {'category_name':'Trial2', 'Sum':350]

How can i achieve this? Was thinking to get foreach loop and then make compare of category_name and use .=+ to get sum? but i lost there,

Thanks,

图片转代码服务由CSDN问答提供 功能建议

我有以下数组,

 数组([0]  =>数组([report_id] => 1 [amount] => 100.00 [category_name] => Trial1)[1] =>数组([report_id] => 1 [数量] => 150.00 [  category_name] => Trial2)[2] =>数组([report_id] => 1 [金额] => 200.00 [category_name] => Trial2)
   
 \  n 

我希望发送给JSON的格式如下

它将获得一些Equal类别名称,然后将其作为json发送。 < pre> [{'category_name':'Trial1','Sum':100]},{'category_name':'Trial2','Sum':350]

我如何实现这一目标? 想要获取 foreach 循环,然后比较category_name并使用。= +来获得总和?但我输了, \ n

谢谢,

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongshi1188 2016-06-06 10:19
    已采纳

    Try below solution:

    <?php
    $array = array ( 
    '0' => Array ( 'report_id' => 1, 'amount' => '100.00', 'category_name' => 'Trial1' ) ,
    '1' => Array ( 'report_id' => 1, 'amount' => '150.00' ,'category_name' => 'Trial2' ),
    '2' => Array ( 'report_id' => 1, 'amount' => '200.00' ,'category_name' => 'Trial2' ) ,
    );
    
    $new_array = array();
    foreach($array as $a){
        if(!isset($new_array[$a['category_name']]['amount'])){
            $new_array[$a['category_name']]['amount'] = 0;
        }
        $new_array[$a['category_name']] = array(
            'category_name' => $a['category_name'],
            'amount' => $new_array[$a['category_name']]['amount'] + $a['amount'],
        );
    }
    
    //print_r(array_values($new_array));
    
    echo json_encode(array_values($new_array));
    

    Output

    [{"category_name":"Trial1","amount":100},{"category_name":"Trial2","amount":350}]
    
    点赞 打赏 评论
  • doulu8415 2016-06-06 10:22

    Possible solution:

    $categoriesArray = array();
    
    foreach ($yourArray as $arrayItem) {
       if (!isset($categoriesArray[$arrayItem['category_name']])) {
           $categoriesArray[$arrayItem['category_name']] = array(
               'category_name' => $arrayItem['category_name'],
               'sum' => 0
           );
       }
    
       $categoriesArray[$arrayItem['category_name']]['sum'] += $arrayItem['amount'];
    }
    
    $categoriesArray = json_encode(array_values($categoriesArray));
    
    点赞 打赏 评论
  • dtkf64283 2016-06-06 10:30

    Assuming $input is your array and $output is the JSON string:

    $categorysum = [];
    array_walk($input, function($el) use (&$categorysum) {
        $categorysum += [$el['category_name'] => ['category_name' => $el['category_name'], 'Sum' => 0]];
        $categorysum[$el['category_name']]['Sum'] += $el['amount'];
    });
    $output = json_encode(array_values($categorysum));
    
    点赞 打赏 评论

相关推荐 更多相似问题