Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
How can I refactor this variables assignment in foreach loop code?

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

Inspired by this.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!