There is an array of goods in the basket.

If you output an array, you get this

Array ( [02db2e33f6361f61182f0226bff9796b] => Array ( [rowid] => 02db2e33f6361f61182f0226bff9796b [id] => 349 [qty] => 1 [price] => 14000 [name] => Dailies Total 1 [images] => e0cf9568ddbf6c809e4f79d13fdedadc.jpg [discount] => 0 [options] => Array ( [optical] => -01.00 [radius] => 8.5 [optical_power] => [os_grad] => [color] => ) [subtotal] => 14000 ) [0ac30b646b9d132a26080046573a91ee] => Array ( [rowid] => 0ac30b646b9d132a26080046573a91ee [id] => 349 [qty] => 1 [price] => 14000 [name] => Dailies Total 1 [images] => e0cf9568ddbf6c809e4f79d13fdedadc.jpg [discount] => 0 [options] => Array ( [optical] => -01.25 [radius] => 8.5 [optical_power] => [os_grad] => [color] => ) [subtotal] => 14000 ) [1482d85f3864ac22acea2a7b326d72a5] => Array ( [rowid] => 1482d85f3864ac22acea2a7b326d72a5 [id] => 13 [qty] => 1 [price] => 4580 [name] => ACUVUE 2 6шт. [images] => 00fe0b832ea22485b52523b853503682.jpeg [discount] => 0 [options] => Array ( [optical] => -00.50 [radius] => 8.3 [optical_power] => [os_grad] => [color] => ) [subtotal] => 4580 ) ) 

There are three products. The two items have the same id field. I need to iterate over the entire array and if the same id then assign the field discount = 5.

How to do?

    1 answer 1

    Surely not the best solution, but here's what happened.

     // Временный массив. Записываем туда шифр товара и id $temp = array(); // Перебираем массив товаров. На один id может быть несколько товаров. Складываем их foreach ($items as $item => $fields) { $temp[$items[$item]["id"]][] = $item; } // Перебираем временный массив. foreach ($temp as $id => $tempItems) { // Если товаров с одинаковым id больше одного, то меняем discount if (count($temp[$id]) > 1) { foreach ($tempItems as $tempItem) { $items[$tempItem]["discount"] = 5; } } } 

    The result of the work . Kind of like you wanted.