tags:

views:

85

answers:

6

Hello,

I have an array

$hourly = array(
        "01" => "0",
        "02" => "0",
        "03" => "0",
        "04" => "0",
        "05" => "0",
        "06" => "0",
        "07" => "0",
        "08" => "0",
        "09" => "0",
        "10" => "0",
        "11" => "0",
        "12" => "0",
        "13" => "0",
        "14" => "0",
        "15" => "0",
        "16" => "0",
        "17" => "0",
        "18" => "0",
        "19" => "0",
        "20" => "0",
        "21" => "0",
        "22" => "0",
        "23" => "0"
            );

And I have a bunch of data like "01" and "03" and "21" and I want to add (+1) to that specific value in the array. So with the data set "01","03","21","01","22" the resulting array would be

$hourly = array(
        "01" => "2",
        "02" => "0",
        "03" => "1",
        "04" => "0",
        "05" => "0",
        "06" => "0",
        "07" => "0",
        "08" => "0",
        "09" => "0",
        "10" => "0",
        "11" => "0",
        "12" => "0",
        "13" => "0",
        "14" => "0",
        "15" => "0",
        "16" => "0",
        "17" => "0",
        "18" => "0",
        "19" => "0",
        "20" => "0",
        "21" => "1",
        "22" => "1",
        "23" => "0"
            );

How could I go about doing that? Is there a function to add 1 to an array element?

A: 

If you simply want to increment each element in the array, you could use:

<?php

    foreach($hourly as &$element) {
        $element++;
    }

?>

However, if you only want to increment elements that hold a value, you can use:

<?php

    foreach($hourly as &$element) {
        $element = $element ? $element + 1 : $element;
    }

?>
middaparka
I want to take a values given and increment only those ("01","03","21","01","22") for example. And I was wondering if there is anyway to avoid having to loop through the entire array for each value and add +1 to that specific value, as the list of data can get extremely long.
Steven
Nope - there's no means of doing this without iterating. (That said, you could of course simply append the value using a getter and leave the array itself intact.) What's this code for? (I'm guessing sometime time based?)
middaparka
+5  A: 
$updates = array("01","03","21","01","22");
foreach($updates as $num) {
    $hourly[$num]++;
}
Scott
A: 
$hours = array("01", "03", "21" );
foreach($hours as $hour) {
   $hourly[$hour] += 1;
}
Mark Baker
A: 

Normally, you'd be able to do:

$array["key"]++;

However, your arrays have a few peculiarities you should fix;

  • Your key values are actually strings. If you want numbers you can increment, you should use numbers from the beginning. If you store a string and increment it with the syntax above, it'll be turned into an integer.
  • Arrays store string or a numbers as keys. You're using both. "01" will be stored as a string key, "10" will be stored as a number. Consider storing only numbers as keys.

None of these make your script not work, but the inconsistency and unnecessary performance hit are avoidable.

Artefacto
A: 

You could construct an array with the updated info ($new = array('01' => '02','03' => '01',) etc and then use array_replace($hourly,$new) or array_merge($hourly,$new). There are probably alternative functions too.

Alan
A: 

You can use a completely "functional" approach, even if it's PHP and even id its not very beautiful ;-) But it works (PHP >= 5.3.0):

...

$fr = 1; $to = 23;

# generate original Array
$hourly = array_combine(
  array_map( function($n){return sprintf("%02s",$n);}, range($fr,$to) ), # Keys
  array_map( function($n){return 0;}, range($fr,$to) )                   # Values
);

$updates = Array('01','03','21','01','22');

# update values based on keys found in $updates
array_walk( $updates, function($u){global $hourly; $hourly[$u]++;} );

...

Regards

rbo

rubber boots