How to loop through object with array with object syntax?

javascript loop through array of objects
javascript loop through array of objects es6
loop through object javascript es6
javascript iterate object key, value
object.entries filter
object.entries typescript
object to array javascript
es6 iterate over object

I would like to loop through my object with object syntax - foreach .. $prop->val but I get an error that it is not an object.

Here is my object:

stdClass Object
(
    [calls] => Array
        (
            [0] => stdClass Object
                (
                    [employee_id] => 47
                    [call_duration] => 10
                )

            [1] => stdClass Object
                (
                    [employee_id] => 47
                    [call_duration] => 10
                )

            [2] => stdClass Object
                (
                    [employee_id] => 47
                    [call_duration] => 137
                )

It was created like this:

$all_calls->calls[] = [
   'employee_id' => $employee_id,
   'call_duration'  => $value->duration
];

How can I loop through "calls" without using array syntax $call["employee_id"] but instead $call->employee_id?

[Edited] I have made same object that you have mentioned and working fine for me;

check below code.

<?php
$all_calls = new stdClass;

$all_calls->calls = [
    (object)['employee_id' => 47, 'call_duration'  => 10],
    (object)['employee_id' => 47, 'call_duration'  => 10],
    (object)['employee_id' => 47, 'call_duration'  => 137],
];

echo "<pre>";
print_r($all_calls);


foreach($all_calls->calls as $call) {
    $employee_id = $call->employee_id;
}

Object.entries(), (The only important difference is that a forin loop enumerates properties in the prototype chain as well). The order of the array returned by Object  A better way to loop through objects. The better way to loop through objects is first to convert the object into an array. Then, you loop through the array. You can convert an object into an array with three methods: Object.keys; Object.values; Object.entries; Object.keys. Object.keys creates an array that contains the properties of an object. Here’s an example.

based on how you create the array of calls, you can do this to iterate over your calls and use them as objects :

foreach($all_calls->calls as $call) {
    $call = (object)$call; // cast the array as an object
    $employeeId = $call->employee_id;
}

On line 2, the associative array will be converted to an object with properties named by keys and corresponding values

reference here : http://php.net/manual/language.types.object.php

Looping through objects in JavaScript, keys creates an array that contains the properties of an object. Here's an example​. const fruits = { apple: 28, orange:  In this tutorial, we are going to learn different ways to loop through an array of objects in JavaScript. First way: ForEach method. In es6 we have a forEach method which helps us to iterate over the array of objects.

Since you don't actually have an array of objects but instead an array of key => value pairs when you do

$all_calls->calls[] = [
   'employee_id' => $employee_id,
   'call_duration'  => $value->duration
];

you will of course not be able to access the members as objects.

You can cast the key => value array to an object when you fetch it, but the more correct thing, especially if you want the entire object structure look like your example is to add instances of stdClass instead of key => value pair arrays to the $calls array from the start. Otherwise, your object will still contain arrays of key => value pairs that you just cast when you want to use them.

You could do something like

$c = new stdClass();
$c->employee_id = 10;
$c->call_duration = 60;
$obj->calls[] = $c

But to make it a bit more compact and not have to type so much you could do something like.

$obj->calls[] = (object)['employee_id' => 10, 'call_duration' => 42];

This will make your $calls member variable to contain instances of stdClass Object whose members can be accessed with ->.

One can also accomplish the same by having the initial data in json format and doing a jsondecode similar to

$obj->calls[] = json_decode('{ "employee_id ": 10, "call_duration": 60 }');

But the other solution is a bit more clear to me.

How to loop through object with array with object syntax?, [Edited] I have made same object that you have mentioned and working fine for me;. check below code. <?php $all_calls = new stdClass;  C# loop through array of objects. I have an array that contains 2 elements and each element can contain one or more object with two properties: Id -> (string) and Names -> (List<string>). You'll probably understand better by taking a look at the following image:

Iterating over arrays and objects in JavaScript, Syntax for (variable in object) statement. Rules: Iterate over property keys, including inherited ones. Don't use for arrays. It iterates over both  In C/C++ sizeof. always gives the number of bytes in the entire object, and arrays are treated as one object. Note: sizeof a pointer--to the first element of an array or to a single object--gives the size of the pointer, not the object (s) pointed to. Either way, sizeof does not give the number of elements in the array (its length). To get the

Ways to Loop Through Objects in JavaScript, We can then use array looping methods such as forEach, map and reduce to retrieve the values of each key. Here's an example: Let me explain. Object.keys  As you can see the for loop statement uses three expressions: the initialization, the condition, and the final expression. The final expression is executed at the end of each loop execution. It is commonly used to increment the index. The following is an example of using JavaScript to loop through an array.

Looping over arrays and objects in JavaScript - Chingu, This data can come in the form of arrays, lists, maps, or other objects. The loop then iterates over all enumerable properties of the object. ArrayList index starts from 0, so we initialized our index variable i with 0 and looped until it reaches the ArrayList size – 1 index. Inside the loop we print the elements of ArrayList using the get method. Using enhanced for loop. Iterating over ArrayList using enhanced for loop is a bit different from iterating ArrayList using for loop

Comments
  • you can cast your array to an object using $call = (object) $call
  • That gives me Trying to get property 'employee_id' of non-object
  • Edited my answer; used same object that you have made and it gives me correct output.
  • How would you later add another entry to calls?
  • didn't get you; can you please explain more.
  • Like this.. . but it only overwrites it... instead of adds.. :( $all_calls->calls = [ (object)['employee_id' => 47, 'call_duration' => 10] ]; $all_calls->calls = [ (object)['employee_id' => 047, 'call_duration' => 100] ];
  • Thank you that's what I was looking for.