0

I have an associative array object :

   [ {
      "id": 15,
      "owner_id": 1,
      "container_info": {
        "id": 1,
        "container_id": 15
      },
      "filters": [
        {
          "id": 3,
          "parent_id": null
        },
        {
          "id": 6,
          "parent_id": null
        }
      ],
      "children_recursive": [
        {
          "id": 7,
          "owner_id": 1,
          "container_info": null,
          "filters": [

          ],
          "children_recursive": [
            {
              "id": 8,
              "owner_id": 1,
              "container_info": null,
              "filters": [

              ],
              "children_recursive": [

              ]
            }
          ]
        },
        {
          "id": 16,
          "owner_id": 1,
          "container_info": {
            "id": 2,
            "container_id": 16
          },
          "filters": [

          ],
          "children_recursive": [

          ]
        },

      ]
    }
   ]

I want to recursively loop through all object and their children_recursive key. And each children_recursive object (at any depth) needs to be processed

So I used :

public function traverseContainerRecursively($containerItems)
    {


            Log:info(' CHECK 1');
              foreach ($containerItems as $containerItem) {

                Log::info(json_encode($containerItem->id));                    
                Log::info(json_encode($containerItem->owner_id));                    
                Log::info(json_encode($containerItem->container_info));
                Log::info(json_encode($containerItem->children_recursive));

              }


              Log::info(' CHECK 2');
              foreach ($containerItems as $containerItem) {

                Log::info(json_encode($containerItem['id']));
                Log::info(json_encode($containerItem['owner_id']));
             Log::info(json_encode($containerItem['container_info']));
         Log::info(json_encode($containerItem['children_recursive']));
              }

            Log::info(' CHECK 3');

            foreach ($containerItems as $key=>$value) {

                    if( $key == "children_recursive" ) {

                            Log::info(json_encode($value));
                            $this->traverseContainerRecursively($value);
                    }

            } //foreach end

    }

OUTPUT :

CHECK 1 
15
1
null
null


CHECK 2 
15
1
null
null

CHECK 3

{
  "id": 15,
  "owner_id": 1,
  "container_info": {
    "id": 1,
    "container_id": 15
  },
  "filters": [
    {
      "id": 3,
      "parent_id": null
    },
    {
      "id": 6,
      "parent_id": null
    }
  ],
  "children_recursive": [
    {
      "id": 7,
      "owner_id": 1,
      "container_info": null,
      "filters": [

      ],
      "children_recursive": [
        {
          "id": 8,
          "owner_id": 1,
          "container_info": null,
          "filters": [

          ],
          "children_recursive": [

          ]
        }
      ]
    },
    {
      "id": 16,
      "owner_id": 1,
      "container_info": {
        "id": 2,
        "container_id": 16
      },
      "filters": [

      ],
      "children_recursive": [

      ]
    },

  ]
} // i.e. the entire passed object

So I am unable to retrieve the value for key "children_recursive". Please guide.

3
  • try with in the if as $value->children_recursive == "children_recursive" Commented Nov 11, 2016 at 7:56
  • not working.. it gives error : ErrorException: Trying to get property of non-object Commented Nov 11, 2016 at 8:10
  • try with in the if as $value['children_recursive'] in Log::info() Commented Nov 11, 2016 at 8:19

1 Answer 1

1

I figured out the issue.

Following code worked:

$obj = json_decode($containerItem);
Log::info(json_encode($obj->children_recursive));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.