How to sum array value of duplicate data

前端 未结 2 1292
鱼传尺愫
鱼传尺愫 2020-12-20 01:35

I have an array with some of same ID value as shown in below.

[
    {\"ID\":\"126871\",\"total\":\"200.00\",\"currency\":\"USD\",\"name\":\"John         


        
相关标签:
2条回答
  • 2020-12-20 02:02

    You will need to:

    • Convert your json string to a php array with json_decode.
    • Loop through the rows and group them using compound temporary keys. In other words generate a single string from each row's ID & currency values and use that string as the temporary unique key.
    • Sum the grouped row's total values.
    • Then prepare the output array for its return to json by reindexing the rows and calling json_encode().

    Code: (Demo)

    $json='[
        {"ID":"126871","total":"200.00","currency":"USD","name":"John"},
        {"ID":"126872","total":"2000.00","currency":"Euro","name":"John"},
        {"ID":"126872","total":"1000.00","currency":"Euro","name":"John"},
        {"ID":"126872","total":"500.00","currency":"USD","name":"John"},
        {"ID":"126872","total":"1000.00","currency":"Euro","name":"John"}
    ]';
    
    $array=json_decode($json,true);  // convert to array
    foreach($array as $row){
        if(!isset($result[$row['ID'].$row['currency']])){
            $result[$row['ID'].$row['currency']]=$row;  // on first occurrence, store the full row
        }else{
            $result[$row['ID'].$row['currency']]['total']+=$row['total'];  // after first occurrence, add current total to stored total
        }
    }
    $result=json_encode(array_values($result));  // reindex the array and convert to json
    echo $result;  // display
    

    Output:

    [
        {"ID":"126871","total":"200.00","currency":"USD","name":"John"},
        {"ID":"126872","total":4000,"currency":"Euro","name":"John"},
        {"ID":"126872","total":"500.00","currency":"USD","name":"John"}
    ]
    
    0 讨论(0)
  • 2020-12-20 02:21

    @Cloud I have made function for your requirement and Thanks @M. I. for look into this sum section.

    $array = array(
        array("ID"  => "126871","total"=>"200.00","currency"=>"USD","name"=>"John"),
        array("ID"  => "126872","total"=>"2000.00","currency"=>"Euro","name"=>"John"),
        array("ID"  => "126872","total"=>"1000.00","currency"=>"Euro","name"=>"John"),
        array("ID"  => "126872","total"=>"500.00","currency"=>"USD","name"=>"John"),
        array("ID"  => "126872","total"=>"1000.00","currency"=>"Euro","name"=>"John"),
    );
    echo "<pre>";
    print_r($array);
    
    function unique_multidim_array($array, $key,$key1,$addedKey) { 
        $temp_array = array(); 
        $i = 0; 
        $key_array = array(); 
        $key1_array = array(); 
    
        foreach($array as $val) { 
            if (!in_array($val[$key], $key_array) && !in_array($val[$key1], $key1_array)) { 
                $key_array[$i] = $val[$key]; 
                $key1_array[$i] = $val[$key1]; 
                $temp_array[$i] = $val; 
            }else{
                $pkey = array_search($val[$key],$key_array);
                $pkey1 = array_search($val[$key1],$key1_array);
                if($pkey==$pkey1){
                    $temp_array[$pkey][$addedKey] += $val[$addedKey];
                }else{
                    $key_array[$i] = $val[$key]; 
                    $key1_array[$i] = $val[$key1]; 
                    $temp_array[$i] = $val; 
                }
                // die;
            }
            $i++; 
        } 
        return $temp_array; 
    } 
    
    $nArray = unique_multidim_array($array,"ID","currency","total");
    // die;
    print_r($nArray);
    die;
    
    0 讨论(0)
提交回复
热议问题