I have the following code, and I would like to get away from call-by-reference time (for converting from 5.2 to 5.3), but I'm not sure exactly what way to do this would be (class, global variable ,?)
Here is the code that should have everything in it http://codepad.org/ombgFPMR
<?php function count_things($item, $key, $total) { $total++; } $counts = array(100 => 1, 101 => 1, 102 => array( 106 => 1, 107 => 1 ), 103 => 1, 104 => 1, 105 => array( 108 => 1, 109 => array( 110 => 1, 111 => 1, 112 => 1 ) ) ); foreach($counts as $key => $count) { $total = 0; if(is_array($count)) { $total++; array_walk_recursive($count, 'count_things', &$total); } else { $total = $count; } $final_counts[$key]['total'] = $total; } print_r($final_counts); ?>
The result looks like this:
Array ( [100] => Array ( [total] => 1 ) [101] => Array ( [total] => 1 ) [102] => Array ( [total] => 3 ) [103] => Array ( [total] => 1 ) [104] => Array ( [total] => 1 ) [105] => Array ( [total] => 5 ) )
source share