A
A
Anton2018-10-01 22:37:00
1C-Bitrix
Anton, 2018-10-01 22:37:00

How to remove a duplicate in an array?

Simplified version of the question

spoiler
<?
$obBasket = \Bitrix\Sale\Basket::getList(array('filter' => array('ORDER_ID' => 13329)));
while($bItem = $obBasket->Fetch())
{echo $bItem[PRODUCT_ID].' - '.$bItem[QUANTITY].'</br>';};
?>
покажет 
30955 - 1
30955 - 1
154835 - 1
А как сделать так?:
30955 - 2
154835 - 1

Full version of the question
spoiler
<?
$obBasket = \Bitrix\Sale\Basket::getList(array('filter' => array('ORDER_ID' => $orderId)));
while($bItem = $obBasket->Fetch()){
$price_s = round($bItem[PRICE],2);
$kolvo_s = round($bItem[QUANTITY],2);
{?>$.post(
                "https://www.google-analytics.com/collect",
                {
                        v: '1',
                        tid: 'UA-xxxxxxxxxxxx-1',
                        cid: '<?=$result_idcode?>',
                        t: 'item',
                        ti: '<?=$orderId?>',
                        in: '<?=$bItem[NAME]?>',
                        ip: '<?=$price_s?>',
                        iv: '<?=$nn?>',
                        iq: '<?=$kolvo_s?>',
                        ic: '<?=$bItem[PRODUCT_ID]?>'
                },
        )
<?};
}
?>

This code in my case sometimes displays the same product twice (as it should be, just the product can have different properties).
So, getting into analytics, a product with the same ID is excluded from google statistics
. Therefore, I want to make sure that if there is more than one product with the same ID in the array, then display the product once, and add the number of duplicates to iq.
Now like this:
spoiler
$.post(
                "https://www.google-analytics.com/collect",
                {
                        v: '1',
                        tid: 'UA-xxxxxxxxxxxx-1',
                        cid: 'xxxxxxxxxxxx.xxxxxxxxxxxx",
                        t: 'item',
                        ti: '1000', //ID номер заказа
                        in: 'Товар 1', //название
                        ip: '999', // цена
                        iv: 'Категория',
                        iq: '1',  // Количество этого товара
                        ic: '10020' // ID Товара
                },
        )
$.post(
                "https://www.google-analytics.com/collect",
                {
                        v: '1',
                        tid: 'UA-xxxxxxxxxxxx-1',
                        cid: 'xxxxxxxxxxxx.xxxxxxxxxxxx",
                        t: 'item',
                        ti: '1000', //ID номер заказа
                        in: 'Товар 2', //название
                        ip: '100', // цена
                        iv: 'Категория Б',
                        iq: '1',  // Количество этого товара
                        ic: '10099' // ID Товара
                },
        )
$.post(
                "https://www.google-analytics.com/collect",
                {
                        v: '1',
                        tid: 'UA-xxxxxxxxxxxx-1',
                        cid: '<?=$result_idcode?>',
                        t: 'item',
                       ti: '1000', //ID номер заказа
                        in: 'Товар 2', //название
                        ip: '100', // цена
                        iv: 'Категория Б',
                        iq: '1',  // Количество этого товара
                        ic: '10099' // ID Товара
                },
        )

As you can see, three products were displayed, the two lower ones are the same, how not to display a duplicate, but simply add an indication of the quantity to the second product iq: '2', // Количество этого товара
So that in the end it would be like this:
spoiler
$.post(
                "https://www.google-analytics.com/collect",
                {
                        v: '1',
                        tid: 'UA-xxxxxxxxxxxx-1',
                        cid: '<?=$result_idcode?>',
                        t: 'item',
                        ti: '1000', //ID номер заказа
                        in: 'Товар 1', //название
                        ip: '999', // цена
                        iv: 'Категория',
                        iq: '1',  // Количество этого товара
                        ic: '10020' // ID Товара
                },
        )
$.post(
                "https://www.google-analytics.com/collect",
                {
                        v: '1',
                        tid: 'UA-xxxxxxxxxxxx-1',
                        cid: 'xxxxxxxxxxxx.xxxxxxxxxxxx",
                        t: 'item',
                        ti: '1000', //ID номер заказа
                        in: 'Товар 2', //название
                        ip: '100', // цена
                        iv: 'Категория Б',
                        iq: '2',  // Количество этого товара - ДОБАВИЛИ КОЛИЧЕСТВО
                        ic: '10099' // ID Товара
                },
        )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2018-10-02
@AlexeyCaTHaR

And why not fill in a temporary array in the loop, in which the product IDs will be the keys, and in the value of the parameters and when the product is duplicated, just increase the number?
Well, then on the array already build the data output

A
Andrey Nikolaev, 2018-10-04
@gromdron

As for the head-on question: why not use the SUM aggregate function to calculate the total and group by PRODUCT_ID?
As for the formal side: this is not the best option, because you can have 2 products sold at different prices. Those. goods 3022 sold at 130 rubles in the amount of 2 pieces and 1 more piece. goods 3022 for 100 rubles (for example, when buying 2x, a discount on the third item is 30 rubles).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question