0

my code:

$atts->each(function($row){
  if($row->key == 30){
    $flag = $row->value;
    echo $flag;
  }
});

The value of $flag will be printed to the site. However, when I try to use the variable $flag outside the loop the variable is unknown.

Can someone tell me what I am missing or what has to be done to have access to that variable?

Thanks in advance!

Regards,

Andreas

1 Answer 1

3

You would need to define that variable in the current scope and import that variable into your callback's scope with use.

$flag = null;
//Access flag by reference
$atts->each(function($row) use (&$flag) {
   ...
});

Since we're using Laravel Collections, I suggest you not use each() for this.

if ($row = $atts->where('key', 30)->first()) {
   $flag = $row->value;
}

This assumes there is only one row with a key of 30 though.

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

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.