-1

This my array example

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

And i want to group if product value same, like this,

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 )
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Your desired output damages the relationships established in the input. If you no longer wish to respect the first level's `_id` values, then you should simplify your desured output structure to be the deeper product ids and their respective count. – mickmackusa Oct 04 '20 at 06:19
  • Your question is missing its coding attempt and/or proof of research/toil -- this makes your question a "requirements dump" (which is a form of volunteer abuse). Look at it this way: you only provided sample input and desured output, then you waited for someone else to do uour work for you. Volunteers must not be used as free code writers. – mickmackusa Oct 04 '20 at 06:55
  • I'm sorry, I am a beginner, and a beginner on stackoverflow, thank you for the advice – Kodkustik ID Oct 04 '20 at 07:12

2 Answers2

1

It makes no sense to retain the first level ids since you are arbitrarily trashing some of the first level ids (damaging the relationships) during the merging process.

Instead I recommend that you only isolate the data that is accurately related.

If this output does not serve your needs, then I'll ask for further question clarification.

By assigning temporary keys to your output array, the output array also acts as a lookup array by which you can swiftly check for uniqueness. The "null coalescing operator" (??) sets a fallback value of 0 when an id is encountered for the first time -- this prevents generating any warnings regarding undeclared keys.

Code: (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);

Output:

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

If you insist of the desired output in your question, then it can be as simple and efficient as this...

Code: (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));

Output:

array (
  0 => 
  array (
    '_id' => '5f76b1788ee23077dccd1a2c',
    'product' => 
    array (
      '_id' => '5d0391a4a72ffe76b8fcc610',
    ),
    'count' => 1,
  ),
  1 => 
  array (
    '_id' => '5f76b6288ee2300700cd1a3a',
    'product' => 
    array (
      '_id' => '5d0391b6a72ffe76b8fcc611',
    ),
    'count' => 2,
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

You can try the below code it will work for you :-)

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

This will produce below Output

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

Rabby
  • 322
  • 4
  • 15
  • 1
    Code-only answers are low value on Stackoverflow because they do very little to educate/empower the OP and thousands of future researchers. – mickmackusa Oct 04 '20 at 06:54
  • `array_search()` (value-based searches) will always perform worse than key-based searches. Your snippet is not respecting the `count` value -- it is merely incrementing by 1. – mickmackusa Oct 04 '20 at 07:18