0

Is there any counterpart for the following statement in JS:

foreach (['a'=>1,'b'=>2] as $k->$v) {
        $$k=$v;
}

You may suggest the following code. But it declares the variables in the global scope.

for(k in {a: 1, b: 2})
  {
      window[k]=this[k];
  }
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173

1 Answers1

0

You should not use variable variables in PHP anyway. Use arrays instead. window[k] is an example of using an array instead (well, an object, but close enough).

As you discovered, there is no way to use local scope variables in JavaScript, so do yourself a favour, use objects ;)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Please help community moderation by voting to close as a duplicate rather than posting your own answer. Also, I'm not sure this really _is_ an answer. – John Dvorak Jan 25 '14 at 03:36