PHP - 如果数组中的值相同,如何分组

PHP - How To Grouping if value same in array

这是我的数组示例

Array
(
  [0] => Array ( [_id] => 5f76b1788ee23077dccd1a2c [product] => Array ( [0] => Array ( [_id] => 5d0391a4a72ffe76b8fcc610 ) ) [count] => 1 )
  [1] => Array ( [_id] => 5f76b6288ee2300700cd1a3a [product] => Array ( [0] => Array ( [_id] => 5d0391b6a72ffe76b8fcc611 ) ) [count] => 1 )
  [2] => Array ( [_id] => 5f76d2488ee23083d3cd1a4a [product] => Array ( [0] => Array ( [_id] => 5d0391b6a72ffe76b8fcc611 ) ) [count] => 1) 
)

如果产品价值相同,我想分组,像这样,

Array
(
  [0] => Array ( [_id] => 5f76b1788ee23077dccd1a2c [product] => Array ( [0] => Array ( [_id] => 5d0391a4a72ffe76b8fcc610 ) ) [count] => 1 )
  [1] => Array ( [_id] => 5f76b6288ee2300700cd1a3a [product] => Array ( [0] => Array ( [_id] => 5d0391b6a72ffe76b8fcc611 ) ) [count] => 2 )
)

您可以尝试下面的代码,它对您有用:-)

<?php
$Myarray = array(['_id'=> '5f76b1788ee23077dccd1a2c', 'product'=> ['_id'=>'5d0391a4a72ffe76b8fcc610'] ,'count'=> 1 ],
['_id'=> '5f76b6288ee2300700cd1a3a', 'product'=> ['_id'=>'5d0391b6a72ffe76b8fcc611'] ,'count'=> 1 ],
['_id'=> '5f76d2488ee23083d3cd1a4a', 'product'=> ['_id'=>'5d0391b6a72ffe76b8fcc611'] ,'count'=> 1 ]
);

$user_array = [];
$temp_array = [];
foreach($Myarray as $temp)
{
    $found = array_search($temp['product']['_id'], $temp_array);
    if($found !== false)
    {
        $i = 0;
        foreach($user_array as $temp1)
        {
            if($temp1['product']['_id'] == $temp['product']['_id'])
            {
                $sum = $temp1['count'] + 1;
                $user_array[$i]['count'] = $sum;
            }
            $i++;
        }
    }
    else
    {
        array_push($user_array,$temp);
        array_push($temp_array,$temp['product']['_id']);
    }
}
print_r($user_array);
?>

这将产生以下输出

Array ( [0] => Array ( [_id] => 5f76b1788ee23077dccd1a2c [product] => Array ( [_id] => 5d0391a4a72ffe76b8fcc610 ) [count] => 1 ) [1] => Array ( [_id] => 5f76b6288ee2300700cd1a3a [product] => Array ( [_id] => 5d0391b6a72ffe76b8fcc611 ) [count] => 2 ) )

保留第一级 ID 毫无意义,因为您在合并过程中任意丢弃了一些第一级 ID(破坏了关系)。

相反,我建议您只隔离准确相关的数据。

如果此输出不能满足您的需求,那么我会要求进一步澄清问题。

通过为输出数组分配临时键,输出数组还充当查找数组,您可以通过它快速检查唯一性。 “null coalescing operator”(??)在第一次遇到 id 时设置回退值 0——这可以防止生成有关未声明键的任何警告。

代码:(Demo)

$array = [
    ['_id' => '5f76b1788ee23077dccd1a2c', 'product' => ['_id'=>'5d0391a4a72ffe76b8fcc610'], 'count'=> 1],
    ['_id' => '5f76b6288ee2300700cd1a3a', 'product' => ['_id'=>'5d0391b6a72ffe76b8fcc611'], 'count'=> 1],
    ['_id' => '5f76d2488ee23083d3cd1a4a', 'product' => ['_id'=>'5d0391b6a72ffe76b8fcc611'], 'count'=> 1]
];

$productCounts = [];
foreach ($array as $item) {
    $productId = $item['product']['_id'];
    $productCounts[$productId] = ($productCounts[$productId] ?? 0) + $item['count'];
}

var_export($productCounts);

输出:

array (
  '5d0391a4a72ffe76b8fcc610' => 1,
  '5d0391b6a72ffe76b8fcc611' => 2,
)

如果你坚持在你的问题中得到想要的输出,那么它可以像这样简单高效...

代码:(Demo)

$result = [];
foreach ($array as $item) {
    $productId = $item['product']['_id'];
    if (!isset($result[$productId])) {
        $result[$productId] = $item;
    } else {
        $result[$productId]['count'] += $item['count'];
    }
}

var_export(array_values($result));

输出:

array (
  0 => 
  array (
    '_id' => '5f76b1788ee23077dccd1a2c',
    'product' => 
    array (
      '_id' => '5d0391a4a72ffe76b8fcc610',
    ),
    'count' => 1,
  ),
  1 => 
  array (
    '_id' => '5f76b6288ee2300700cd1a3a',
    'product' => 
    array (
      '_id' => '5d0391b6a72ffe76b8fcc611',
    ),
    'count' => 2,
  ),
)