Note that there are some explanatory texts on larger screens.

plurals
  1. POphp 5.3 convert array_walk_recursive
    text
    copied!<p>I have the following code, and I'd like to get away from the call-time pass-by-reference, (To convert from 5.2 to 5.3) but I'm not sure exactly sure what the correct way to do this would be (class, global variable, ?)</p> <p>Here is a codepad that should have everything in it <a href="http://codepad.org/ombgFPMR" rel="nofollow">http://codepad.org/ombgFPMR</a></p> <pre><code>&lt;?php function count_things($item, $key, $total) { $total++; } $counts = array(100 =&gt; 1, 101 =&gt; 1, 102 =&gt; array( 106 =&gt; 1, 107 =&gt; 1 ), 103 =&gt; 1, 104 =&gt; 1, 105 =&gt; array( 108 =&gt; 1, 109 =&gt; array( 110 =&gt; 1, 111 =&gt; 1, 112 =&gt; 1 ) ) ); foreach($counts as $key =&gt; $count) { $total = 0; if(is_array($count)) { $total++; /* The below is a logic error. Array elements that contain arrays do not get the callback function called on them. Therefore, any children with children of their own will not be counted. In the output of this paste, the final key, $final_counts[105]['total'], should have a value of 6, but it actually has a value of 5. */ array_walk_recursive($count, 'count_things', &amp;$total); } else { $total = $count; } $final_counts[$key]['total'] = $total; } print_r($final_counts); ?&gt; </code></pre> <p>Output looks like:</p> <pre><code>Array ( [100] =&gt; Array ( [total] =&gt; 1 ) [101] =&gt; Array ( [total] =&gt; 1 ) [102] =&gt; Array ( [total] =&gt; 3 ) [103] =&gt; Array ( [total] =&gt; 1 ) [104] =&gt; Array ( [total] =&gt; 1 ) [105] =&gt; Array ( [total] =&gt; 5 ) ) </code></pre>
 

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