Note that there are some explanatory texts on larger screens.

plurals
  1. POPass multiple variables by reference to a foreach loop (PHP)
    primarykey
    data
    text
    <p><strong>Scenario/Problem Isolation</strong>: Lets suppose my program uses MULTIPLE variables. At the program beginning I want to manipulate MANY of the variables AT ONCE through a general function with LITTLE CODE, before then later in the process using only distinctive few variables in specific functions.</p> <p><strong>Question</strong>: How do I pass multiple variables by reference to a foreach loop? Or is there a better/alternative method for looping through multiple determined variables?</p> <p><strong>Post(s) related to topic</strong>, but didn't solve my issue:</p> <p><a href="https://stackoverflow.com/questions/1186274/php-foreach-loop-on-multiple-objects">PHP foreach loop on multiple objects?</a></p> <p><strong>Background (for those concerned):</strong> I have a command line program which uses getopts <a href="http://hash-bang.net/2008/12/missing-php-functions-getopts/" rel="nofollow noreferrer">http://hash-bang.net/2008/12/missing-php-functions-getopts/</a> to get various arguments, thus I get about 20 variables. I want to run all variables, which contain filepath(s) (about 10) through the "general" function reduceHierarchyDots() at ONCE (instead of calling the function 10 times).</p> <pre><code>&lt;?php /// The "general" function: function reduceHierarchyDots ($file) { while (preg_match('|./\.{2}/|', $file)) { $file = preg_replace('|/([^/]+)/\.{2}/|', '/', $file, 1); } $file = preg_replace('|(/(\./)+)|', '/', $file); $file = preg_replace('|^(\./)+|', '', $file); return $file; } function reduceHierarchyDotsRef (&amp;$file) { while (preg_match('|./\.{2}/|', $file)) { $file = preg_replace('|/([^/]+)/\.{2}/|', '/', $file, 1); } $file = preg_replace('|(/(\./)+)|', '/', $file); $file = preg_replace('|^(\./)+|', '', $file); } /// The "many" variables: $x = "something"; $y = 123; $y = array ("a", "B", 3); $a = "/Users/jondoe/Desktop/source/0.txt"; $b = "/Users/jondoe/Desktop/source/../1.txt"; $c = "/Users/jondoe/Desktop/source/../../2.txt"; $arrOne = array ( "v1" =&gt; "/some/thing/../1.pdf", "v2" =&gt; "/some/thing/../../2.pdf", "v3" =&gt; "/some/thing/../../../3.pdf" ); $arrTwo = array ( "./1.doc", "/so.me/.thing/ends./././2.doc", "./././3.doc" ); /// At the beginning I want to run multiple determined variables through a "general" function: /// Debugging: Variables BEFORE the manipulation: echo("BEFORE:\n"); var_dump($b, $arrOne["v2"], $arrTwo[2]); echo("\n"); /// Method works, but is long! (1 line/statement per function call) reduceHierarchyDotsRef($b); reduceHierarchyDotsRef($arrOne["v2"]); reduceHierarchyDotsRef($arrTwo[2]); /// Hence, I'd like to pass all variables by reference at once to a foreach loop: //// These cause: Parse error: syntax error, unexpected '&amp;': // foreach ( array($b, $arrOne["v2"], $arrTwo[2] ) as &amp;$file) { $file = reduceHierarchyDots($file); } // foreach (array(&amp;$b, &amp;$arrOne["v2"], &amp;$arrTwo[2] ) as &amp;$file) { $file = reduceHierarchyDotsRef($file); } //// These have no effect on the intended variables: // foreach (array(&amp;$b, &amp;$arrOne["v2"], &amp;$arrTwo[2] ) as $file) { $file = reduceHierarchyDots($file); } // foreach (array(&amp;$b, &amp;$arrOne["v2"], &amp;$arrTwo[2] ) as $file) { $file = reduceHierarchyDotsRef($file); } /// Debugging: Variables AFTER the manipulation: echo("AFTER:\n"); var_dump($b, $arrOne["v2"], $arrTwo[2]); /// After the "general" function ran over various variables, the more specific actions happen: ... ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload