K
K
Kirill Gorelov2021-11-18 16:56:16
PHP
Kirill Gorelov, 2021-11-18 16:56:16

Foreach, strange loop behavior?

Array from Bitrix, output a smart filter.

spoiler

Array
(
    [44] => Array
        (
            [ID] => 44
            [IBLOCK_ID] => 4
            [CODE] => P_88172288_8c75_11e6_af60_001e67496364
            [~NAME] => Бренд
            [NAME] => Бренд
            [PROPERTY_TYPE] => L
            [USER_TYPE] => 
            [USER_TYPE_SETTINGS] => 
            [DISPLAY_TYPE] => F
            [DISPLAY_EXPANDED] => Y
            [FILTER_HINT] => 
            [VALUES] => Array
                (
                    [62] => Array
                        (
                            [CONTROL_ID] => ifFilter_44_1233418
                            [CONTROL_NAME] => ifFilter_44_1233418
                            [CONTROL_NAME_ALT] => ifFilter_44
                            [HTML_VALUE_ALT] => 1233418
                            [HTML_VALUE] => Y
                            [VALUE] => AUMA
                            [SORT] => 500
                            [UPPER] => AUMA
                            [FLAG] => 
                            [URL_ID] => a0a1ce5bc50945a844aaaa503d9a09b9
                        )

                )

        )

    [220] => Array
        (
            [ID] => 220
            [IBLOCK_ID] => 5
            [CODE] => P_3ea07275_a385_11e7_a4e0_001e67496365
            [~NAME] => Длина (мм)
            [NAME] => Длина (мм)
            [PROPERTY_TYPE] => N
            [USER_TYPE] => 
            [USER_TYPE_SETTINGS] => 
            [DISPLAY_TYPE] => A
            [DISPLAY_EXPANDED] => 
            [FILTER_HINT] => 
            [VALUES] => Array
                (
                    [MIN] => Array
                        (
                            [CONTROL_ID] => ifFilter_220_MIN
                            [CONTROL_NAME] => ifFilter_220_MIN
                            [VALUE] => 288
                        )

                    [MAX] => Array
                        (
                            [CONTROL_ID] => ifFilter_220_MAX
                            [CONTROL_NAME] => ifFilter_220_MAX
                            [VALUE] => 864
                        )

                )

        )

    [Розничная цена] => Array
        (
            [ID] => 2
            [CODE] => Розничная цена
            [URL_ID] => %D1%80%D0%BE%D0%B7%D0%BD%D0%B8%D1%87%D0%BD%D0%B0%D1%8F%20%D1%86%D0%B5%D0%BD%D0%B0
            [~NAME] => Розничная цена
            [NAME] => Розничная цена
            [PRICE] => 1
            [VALUES] => Array
                (
                    [MIN] => Array
                        (
                            [CONTROL_ID] => ifFilter_P2_MIN
                            [CONTROL_NAME] => ifFilter_P2_MIN
                            [VALUE] => 128278.08
                            [CURRENCY] => RUB
                        )

                    [MAX] => Array
                        (
                            [CONTROL_ID] => ifFilter_P2_MAX
                            [CONTROL_NAME] => ifFilter_P2_MAX
                            [VALUE] => 470995.56
                            [CURRENCY] => RUB
                        )

                )

            [ENCODED_ID] => c81e728d9d4c2f636f067f89cc14862c
            [CURRENCIES] => Array
                (
                    [RUB] => Рубль
                )

        )

)


There is the last element of the array [Retail price],
I do foreach and the last element that "retail price" is not displayed, the penultimate one is displayed again.

debug($arResult["ITEMS"] );
foreach($arResult["ITEMS"] as $key=>$arItem) {
     debug($key);
     debug($arItem);
}


Output of the last iteration
spoiler

Розничная цена
Array
(
[ID] => 220
[IBLOCK_ID] => 5
[CODE] => P_3ea07275_a385_11e7_a4e0_001e67496365
[~NAME] => Длина (мм)
[NAME] => Длина (мм)
[PROPERTY_TYPE] => N
[USER_TYPE] =>
[USER_TYPE_SETTINGS] =>
[DISPLAY_TYPE] => A
[DISPLAY_EXPANDED] =>
[FILTER_HINT] =>
[VALUES] => Array
(
[MIN] => Array
(
[CONTROL_ID] => ifFilter_220_MIN
[CONTROL_NAME] => ifFilter_220_MIN
[VALUE] => 288
)

[MAX] => Array
(
[CONTROL_ID] => ifFilter_220_MAX
[CONTROL_NAME] => ifFilter_220_MAX
[VALUE] => 864
)

)

)


That is, the key displays the correct one, but the element itself does not ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Gorelov, 2021-11-18
@Kirill-Gorelov

Guys, I have not figured out the real reason.
But I fixed it simply:
foreach($arResult["ITEMS"] as $key=>$arItem)
was replaced with
foreach($arResult["ITEMS"] as $key=>$item)

A
Andrey Korekhov, 2021-11-18
@Haotik

So if I'm not mistaken it's not in the $arResult[items] subarray it's the last element of the $arResult array.
Although, according to your example, it should be ...
Sorry, I was mistaken)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question