0

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?

1
  • you can cast your array to an object using $call = (object) $call Commented Sep 11, 2018 at 13:18

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thank you that's what I was looking for.
0

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

10 Comments

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] ];
|

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.