Skip to content
Advertisement

How do you pull this value out of a stdClass Object?

How do I pull the value of the workers, I want the result “iamdamnsam.L3”. I tried $data->workers[0]; but that didn’t work. I am using json_decode() to get $data

JavaScript

Advertisement

Answer

Let’s say there are/will be more workers.

Then you can loop the object of all workers by converting the Object to the array on the fly using Type Casting ie. (array)$object

Start loop on $object->user->workers and then you iterate each worker one by one.

See example bellow:

JavaScript

Output of keys of workers:

JavaScript

Looping all the workers and accessing their properties:

JavaScript

Output of foreach loop:

JavaScript

Live demo

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement