Martin Zeltin
Martin Zeltin

Reputation: 57

How to loop through object with array with object syntax?

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?

Upvotes: 0

Views: 2272

Answers (3)

inquam
inquam

Reputation: 12932

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.

Upvotes: 1

Divyesh Prajapati
Divyesh Prajapati

Reputation: 985

[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;
}

Upvotes: 0

ᴄʀᴏᴢᴇᴛ
ᴄʀᴏᴢᴇᴛ

Reputation: 2999

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

Upvotes: 1

Related Questions