TransWikia.com

Почему приходит объект а не массив?

Stack Overflow на русском Asked on December 16, 2021

Формирую массив и отправляю его с помощью echo json_encode();

В пхп это выглядит вот так:

Array
(
    [NOLIMITS] => 
    [CATEGORIES] => Array
        (
            [1] => Array
                (
                    [ID] => 1367
                    [NAME] => Обед
                    [UF_TIME] => 13:00
                    [SUBCATEGORIES] => Array
                        (
                            [0] => Array
                                (
                                    [ID] => 1376
                                    [NAME] => горячее
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [ID] => 1941
                                                    [NAME] => Баранина тушеная с рагу из баклажанов и кабачков 260/15 
                                                    [PREVIEW_PICTURE] => 873
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/a81/istockphoto_927720126_cr.jpg
                                                )

                                            [1] => Array
                                                (
                                                    [ID] => 1938
                                                    [NAME] => Гречка с грибами и кедровыми орехами 150г 
                                                    [PREVIEW_PICTURE] => 842
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/d4b/1.jpg
                                                )

                                            [2] => Array
                                                (
                                                    [ID] => 1975
                                                    [NAME] => Овощи на пару
                                                    [PREVIEW_PICTURE] => 857
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/078/ovoschi-na-paru_cr.jpg
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [ID] => 1782
                                    [NAME] => первое
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [ID] => 2949
                                    [NAME] => десерт
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                            [3] => Array
                                (
                                    [ID] => 2950
                                    [NAME] => напиток
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                            [4] => Array
                                (
                                    [ID] => 2951
                                    [NAME] => салат
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                        )

                )

            [3] => Array
                (
                    [ID] => 1368
                    [NAME] => Ужин
                    [UF_TIME] => 17:00
                    [SUBCATEGORIES] => Array
                        (
                            [0] => Array
                                (
                                    [ID] => 1376
                                    [NAME] => горячее
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [ID] => 1931
                                                    [NAME] => Бефстроганов 
                                                    [PREVIEW_PICTURE] => 673
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/249/befstroganov.jpg
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [ID] => 2949
                                    [NAME] => десерт
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [ID] => 2950
                                    [NAME] => напиток
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                            [3] => Array
                                (
                                    [ID] => 2951
                                    [NAME] => салат
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [ID] => 1893
                                                    [NAME] => Овощная нарезка 100 г 
                                                    [PREVIEW_PICTURE] => 929
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/fe5/istockphoto_595343954_cr.jpg
                                                )

                                        )

                                )

                        )

                )

            [0] => Array
                (
                    [ID] => 1366
                    [NAME] => Завтрак
                    [UF_TIME] => 10:00
                    [SUBCATEGORIES] => Array
                        (
                            [0] => Array
                                (
                                    [ID] => 1375
                                    [NAME] => основное
                                    [UF_MAX_CHOOSEN_BLUD] => 2
                                    [PRODUCTS] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [ID] => 1880
                                                    [NAME] => Багет 
                                                    [PREVIEW_PICTURE] => 546
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/6cd/baget.jpg
                                                )

                                            [1] => Array
                                                (
                                                    [ID] => 1862
                                                    [NAME] => Блинчики с творогом 140/24г 
                                                    [PREVIEW_PICTURE] => 930
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/9d7/blinchiki-s-tvorogom-140-24g.jpg
                                                )

                                            [2] => Array
                                                (
                                                    [ID] => 1846
                                                    [NAME] => Груша 1 шт 
                                                    [PREVIEW_PICTURE] => 864
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/139/grusha-1-sht_cr.jpg
                                                )

                                            [3] => Array
                                                (
                                                    [ID] => 1859
                                                    [NAME] => Омлет белковый на пару 
                                                    [PREVIEW_PICTURE] => 931
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/4b9/istockphoto_173651967_cr.jpg
                                                )

                                            [4] => Array
                                                (
                                                    [ID] => 1847
                                                    [NAME] => Семга малосольная с перепелиными яйцами 
                                                    [PREVIEW_PICTURE] => 953
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/037/semga-malososnaja-s-perepelinymi-jajcami_cr.jpg
                                                )

                                            [5] => Array
                                                (
                                                    [ID] => 1997
                                                    [NAME] => Яичный пудинг 70/20/16 
                                                    [PREVIEW_PICTURE] => 784
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/7c8/jaichnyj-puding-70-20-16_cr.jpg
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [ID] => 2950
                                    [NAME] => напиток
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [ID] => 2952
                                    [NAME] => фрукт
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [ID] => 2928
                                                    [NAME] => Яблоко зеленое 1 шт 
                                                    [PREVIEW_PICTURE] => 974
                                                    [PREVIEW_TEXT] => 
                                                    [PREVIEW_TEXT_TYPE] => text
                                                    [IMAGE_URL] => http://emcq.zapusq.ru/upload/iblock/e00/jabloko-zelenoe-1-sht_cr.jpg
                                                )

                                        )

                                )

                        )

                )

            [2] => Array
                (
                    [ID] => 2001
                    [NAME] => Полдник
                    [UF_TIME] => 15:00
                    [SUBCATEGORIES] => Array
                        (
                            [0] => Array
                                (
                                    [ID] => 2949
                                    [NAME] => десерт
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [ID] => 2950
                                    [NAME] => напиток
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [ID] => 2952
                                    [NAME] => фрукт
                                    [UF_MAX_CHOOSEN_BLUD] => 1
                                    [PRODUCTS] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

Но в ответе CATEGORIES приходит как объект, а не как массив
Объект, а не массив

2 Answers

Чтобы массив был преобразован функцией json_encode() именно как массив, нужно соблюдать эти требования:

  1. только числовые индексы,
  2. индексы в порядке возрастания,
  3. индексы начинаются с нуля,
  4. индексы пропускать нельзя.

Answered by Denis on December 16, 2021

Попробуйте вынести определение категорию 0 завтрак перед обедом. Идентичный пример с subcategory даёт верный результат.

Answered by Aziz Umarov on December 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP