php - Alternative to foreach pass by reference -


I came from a case where the element is passed from context to modify, then the code Stage, some array is stopped again to make some calculations, but this time passes by the value of the value. The point is that PHP retains the context of the last element in the array in the first phase, then overwrites the element when the next fat starts then when the name of the local variable is the same.

Example Code:

  & lt ;? Php $ a = array ("a" = & gt; "foo"); $ B = array ("b" = & gt; "bar"); $ X = array ($ a, $ b); Forex Currency ($ x & amp; y) {} print_r ($ x); Forex Currency ($ x $ Y) {} print_r ($ x); ? & Gt;  

This will generate

 array ([0] => array ([a] => foo) [1] => array ([b] => bar ) Array ([a] => foo) [1] => Array ([a] => foo)) 

This stupidity has been stated in the PHP manual

P>

Alert remains the reference of a $ value and the last array element even after the foreach loop.

And in fact, by using unset ($ y) this problem will solve. But it is very weak and you always have to set a variable For those who can not trust the memorizing codder, there is no scope. So my question is: Is there any good option for pass-by-reference in future, which later eliminates the need to unset the convertible?

You can use:

  array_walk ($ x, Function (& amp; $ y)) {/ * ... * /});  

This makes the reference $ y local within the scope of the callback function so that the unsetting is handled automatically.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -