3

I have array of objects like so;

Array
(
    [0] => stdClass Object
        (
            [Job] => stdClass Object
                (
                    [ID] => 123
                    [Name] => Foo
                 )
        )
    [1] => stdClass Object
        (
            [Job] => stdClass Object
                (
                    [ID] => 456
                    [Name] => BAR
                 )
        )
)

I need to loop through the array and append some additional information to the object like 'Status', but I'm having some issues.

foreach($arrJobs as $key => $val) {

  $arrJobs[$key]->Job->Status = new StdClass;
  $arrJobs[$key]->Job->Status = $myStatus;

}

This appears to work, but I get the following warning;

Warning: Creating default object from empty value in...

1
  • 1
    No warning emitted here . Can you update your example to one that actually reproduces the problem. Commented Jul 17, 2017 at 10:20

4 Answers 4

2

Yes, create an object first. You cannot assign properties of null. That's why you need an instance of stdClass, php's generic empty class.

$arrJobs[$key] = new stdClass;
$arrJobs[$key]->foo = 1;

// And/or see below for 'nested' ...
$arrJobs[$key]->bar = new stdClass;
$arrJobs[$key]->bar->foo = 1;
Sign up to request clarification or add additional context in comments.

Comments

1

According to my understanding to you question you just need to append properties to your existing objects. Don't create new objects in your loop

you just need this

foreach ($arrJobs as $obj)
{
    $obj->job->status = $myStatus;
}

See the full code :

<?php
$obj1 = new \stdClass();
$obj1->job = new \stdClass();
$obj1->job->id = 123;
$obj1->job->name = "foo";

$obj2 = new \stdClass();
$obj2->job = new \stdClass();
$obj2->job->id = 456;
$obj2->job->name = "bar";

$array = [$obj1,$obj2];

var_dump($array);
foreach ($array as $obj)
{
    $obj->job->status = "the status";
    //add any properties as you like dynamicly here
}
echo "<br>\nafter<br>\n";
var_dump($array);
exit;

Now $obj1 and $obj2 has the new property 'status' ,see that demo : (https://eval.in/833410)

Comments

0
for ($i = 0; $i < count($arrJobs); $i++)
{
    $arrJobs[$i]->Job->Status = new stdClass;
    // other operations
}

Comments

0

Try this,

PHP

<?php
    // Sample object creation.
    $array = [];
    $array = [0 => (object) ['Job'=>(object) ['ID'=>123, 'Name' => 'Foo']]];

    foreach($array as $val) {
        $val->Job->Status = (object) ['zero' => 0,'one' => 1]; // Status 0 and 1.
    }

    echo "<pre>";
    print_r($array);
?>

Output

Array
(
    [0] => stdClass Object
        (
            [Job] => stdClass Object
                (
                    [ID] => 123
                    [Name] => Foo
                    [Status] => stdClass Object
                        (
                            [zero] => 0
                            [one] => 1
                        )

                )

        )

)

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.