php array find duplicates, sum them up & delete duplicates

后端 未结 4 1077
无人共我
无人共我 2020-12-01 20:23

i have an array:

Array 
(
[0] => Array
    (
        [setid] => 2
        [income] => 100
    )

[1] => Array
    (
        [setid] => 2
              


        
4条回答
  •  無奈伤痛
    2020-12-01 20:40

    You should use the array_unique function that php's official site offers.

    An example:

     "green", "red", "b" => "green", "blue", "red");
    $result = array_unique($input);
    print_r($result);
    ?>
    

    Output:

    Array
    (
        [a] => green
        [0] => red
        [1] => blue
    )
    

    To sum the duplicated values you can use the array_count_values function:

    
    

    Output would be:

    Array
    (
        [1] => 2
        [hello] => 2
        [world] => 1
    )
    

提交回复
热议问题