Как получить новый массив, который обьединен по условию в цикле: одинаковый price и user_id,при этом сложить все count?

Array
(
    [0] => Array
        (
            [id] => 1
            [id_user] => 20
            [count] => 2
            [price] => 1000
        )

    [1] => Array
        (
            [id] => 2
            [id_user] => 20
            [count] => 5
            [price] => 1000
        )

    [2] => Array
        (
            [id] => 3
            [id_user] => 21
            [count] => 3
            [price] => 1000
        )

    [3] => Array
        (
            [id] => 4
            [id_user] => 20
            [count] => 3
            [price] => 2000
        )

    [4] => Array
        (
            [id] => 5
            [id_user] => 22
            [count] => 3
            [price] => 3500
        )

    [5] => Array
        (
            [id] => 6
            [id_user] => 21
            [count] => 10
            [price] => 1000
        )

)
======== результат,который хочется получить ==============
Array
(
    [0] => Array
        (
            [id] => 1
            [id_user] => 20
            [count] => 17
            [price] => 1000
        )

    [1] => Array
        (
            [id] => 4
            [id_user] => 21
            [count] => 13
            [price] => 1000
        )

    [2] => Array
        (
            [id] => 2
            [id_user] => 20
            [count] => 4
            [price] => 2000
        )

    [3] => Array
        (
            [id] => 5
            [id_user] => 22
            [count] => 3
            [price] => 3500
        )

)

$arr = [
    ['id' => 1, 'id_user' => 20, 'count' => 2, 'price' => 1000],
    ['id' => 2, 'id_user' => 20, 'count' => 5, 'price' => 1000],
    ['id' => 3, 'id_user' => 21, 'count' => 3, 'price' => 1000],
    ['id' => 4, 'id_user' => 20, 'count' => 3, 'price' => 2000],
    ['id' => 5, 'id_user' => 22, 'count' => 3, 'price' => 3500],
    ['id' => 6, 'id_user' => 21, 'count' => 10, 'price' => 1000],

];

foreach ($arr as $item) {
    foreach ($arr as $item2) {

        if ($item['price'] == $item2['price'] && $item['user_id'] == $item2['user_id'])
            $merge_arr[]['count'] += $item2['count'];
            // .............
    }
}
// результат который хочется получить
$merge_arr = [
    ['id' => 1, 'id_user' => 20, 'count' => 17, 'price' => 1000],
    ['id' => 4, 'id_user' => 21, 'count' => 13, 'price' => 1000],
    ['id' => 2, 'id_user' => 20, 'count' => 4, 'price' => 2000],
    ['id' => 5, 'id_user' => 22, 'count' => 3, 'price' => 3500],
];

print_r($arr);
print_r($merge_arr);
  • Вопрос задан
  • 34 просмотра
Решения вопроса 1
0xD34F
@0xD34F
$merged = [];
foreach ($arr as $item) {
  $key = $item['id_user'].'/'.$item['price'];
  if (array_key_exists($key, $merged)) {
    $merged[$key]['count'] += $item['count'];
  } else {
    $merged[$key] = $item;
  }
}
$merged = array_values($merged);
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы