0

The code is:

foreach($page->items as $item) {
  switch ($item->var) {
    case 'var1': $var1 = $item; break;
    case 'var2': $var2 = $item; break;
    case 'var3': $var3 = $item; break;
    // ...
    case 'varN': $varN = $item; break;
    default: // do nothing
  }
}

I would like to make each item var to be assigned to a "named variable" e.g. $var1 in case of var1.

1 Answer 1

1
foreach($page->items as $item) {
  ${$item->var} = $item;
}

Inspired by this.

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.