-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] => 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 )
)
4

2 回答 2

1

保留第一级 id 是没有意义的,因为您在合并过程中任意丢弃了一些第一级 id(破坏了关系)。

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

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

通过将临时键分配给输出数组,输出数组还可以用作查找数组,您可以通过它快速检查唯一性。“空合并运算符” ( ) 设置第一次遇到 id 时的??回退值- 这可以防止生成任何有关未声明键的警告。0

代码:(演示

$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,
)

如果您在问题中坚持所需的输出,那么它可以像这样简单有效......

代码:(演示

$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,
  ),
)
于 2020-10-04T06:31:56.683 回答
0

您可以尝试以下代码,它将为您工作:-)

<?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);
?>

这将产生以下输出

数组 ( [0] => 数组 ( [_id] => 5f76b1788ee23077dccd1a2c [产品] => 数组 ( [_id] => 5d0391a4a72ffe76b8fcc610 ) [count] => 1 ) [1] => 数组 ( [_id] => 5f76b6288ee2300700cd1a3a [产品] => 数组 ([_id] => 5d0391b6a72ffe76b8fcc611) [count] => 2))

于 2020-10-04T05:35:48.190 回答