1

I have an array of objects (from DB) and need to foreach this one before send to the viewer:

$data['contracts'] = array();

foreach ($contracts as $c) {
    $data['contracts'][] = array(
         'id' => $c->id,
         'num' => $c->num,
         'delay' => function ($c->date_added) {
              ... blablabla ... 
         },
    );
}

This examples returns an error because $c->date_added is uses, as workaround I must define additional variable before foreach loop:

$date_added = $c->date_added;

How can I use properties in anonymous functions without additional variables?

1 Answer 1

1

This might be easier:

// more stuff
'delay' => function ($c) { 
    $dateAdded = $c->date_added;
    // rest of bla bla bla...
},
// more stuff
Sign up to request clarification or add additional context in comments.

1 Comment

I bet you mistyped here->-> ;)

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.