Verification: a143cc29221c9be0

Php add one value to array

If you want to preserve the leading zeros in the indexes

The following will only work if the inputs are really in an array and the values are consistent (ie "one" always occurs as "01" and never as "1" (or vice-versa)):

$counts = array_count_values( $inputs );

Because of the leading zeros for values $counts["01"] not $counts["1"]. If you only care about the values that have been seen, a simple will suffice:

foreach( $counts as $value => $count ){ print "$value => $count\n"; }

If you are going to iterate through every possible value (or otherwise access them randomly), you need a bit of a kludge:

function lpad( $index ){ return str_pad($index,2,"0",STR_PAD_LEFT); }

or if you want to avoid a function call for performance

$lpad = array_keys($hourly); ### IMPORTANT! ASSUMES $hourly ACTUALLY STARTS WITH "00"

If you have a version of PHP that doesn't have ?? the body of the for loop should be something like

print (is_null( $p = $counts[$lpad[$i]] )? 0 : $p )." ";