Home > front end >  How to check if array is same more thane one and sum count value in php
How to check if array is same more thane one and sum count value in php

Time:09-20

I have an array (variable $arr) if there is the same user_id and username than sum count.

This question is only one value is same PHP- how to sum array elements with duplicate element value

I need to check if two value is same not only one

Like this If user_id and username is same than sum count.

You can see example in my code

Before

$arr = [
      [
          'user_id'  => 120,
          'username'  => test1,
          'count' => 2
      ],
      [
          'user_id'  => 120,
          'username'  => test1,
          'count' => 3
      ],
      [
          'user_id'  => 110,
          'username'  => test2,
          'count' => 2
      ]
  ];

After (If user_id and username is same then sum count)

$arr = [
      [
          'user_id'  => 120,
          'username'  => test1,
          'count' => 5
      ],
      [
          'user_id'  => 110,
          'username'  => test2,
          'count' => 2
      ]
  ];

CodePudding user response:

Create an empty array and use user_id and username as index (key) to sum them up.

$summedArray = [];
foreach($arr as $item) {
    $key = $item['user_id'] . $item['username'];
    if(!isset($summedArray[$key])) {
        $summedArray[$key] = $item;
        continue;
    }
    $summedArray[$key]['count']  = $item['count'];
}

print_r($summedArray);

Tipp

If you want to get rid of the keys use

$summedArray = array_values($summedArray);

Output

Array
(
    [120test1] => Array
        (
            [user_id] => 120
            [username] => test1
            [count] => 5
        )

    [110test2] => Array
        (
            [user_id] => 110
            [username] => test2
            [count] => 2
        )

)

CodePudding user response:

$arr = [
  [
      'user_id'  => 120,
      'username'  => 'test1',
      'count' => 2
  ],
  [
      'user_id'  => 120,
      'username'  => 'test1',
      'count' => 3
  ],
  [
      'user_id'  => 110,
      'username'  => 'test2',
      'count' => 2
  ]
];

$i = 0;
while($i < count($arr)){
    $k = $i;
    while($k<count($arr) - $i   1 && $k<count($arr)   1){
        if($k != $i && $arr[$i]["user_id"] == $arr[$k]["user_id"]){
            $arr[$i]["count"]  = $arr[$k]["count"];
            unset($arr[$k]);
        }
        $k  ;
    }
    $i  ;
}



print_r($arr);

CodePudding user response:

The built-in array_reduce() can come in handy for this case, basically this function will:

  1. loop through the $arr variable
  2. sum the count values based on the user_id (as you have explained)
  3. return the resulting array.

here's a possible solution with a live demo:

$arr = [
    [
        'user_id' => 120,
        'username' => 'test1',
        'count' => 2
    ],
    [
        'user_id' => 120,
        'username' => 'test1',
        'count' => 3
    ],
    [
        'user_id' => 110,
        'username' => 'test2',
        'count' => 2
    ]
];

/** loop through "$arr" and sum the "count" values based on the "user_id" */
$result = array_reduce($arr, function ($a, $c) {
    /** index the resulting array by the "user_id" value which allows for instant lookup in case of duplicate "user_id" value */
    $a[$c['user_id']] = [
        'user_id' => $c['user_id'],
        'username' => $c['username'],
        /** sum the "count" values
         * the first iteration will have an empty array
         * so we should check for existance before getting the value in order to prevent bugs
         */
        'count' => ($a[$c['user_id']]['count'] ?? 0)   $c['count']
    ];
    return $a;
}, []);

/** print the result */
var_export($request);

The new $result array shall contain:

[
    120 => [
        'user_id' => 120,
        'username' => 'test1',
        'count' => 5,
    ],
    110 => [
        'user_id' => 110,
        'username' => 'test2',
        'count' => 2,
    ],
]
  • Related