Re: Re: help with arrays

From: Date: Thu, 16 May 2002 09:39:18 +0000
Subject: Re: Re: help with arrays
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-97888@lists.php.net to get a copy of this message
>> This is a basic question but I'm a basic fellow. If I have an array >> >> $timespread = array("12am-01am"=>0); >> $timespread["01am-02am"]=0; >> $timespread["02am-03am"]=0; etc >> >> Using $time which is a number, I want to add 1 to the value of >> $timespread[$time] without changing the key so if $time =1 >> I want to have $timespread["01am-02am"]=1; >> >> Using $timespread[$time]+=1; doesn't work. > > Nope, you must say > > $timespread["01am-02am"] += 1 I think the problem here is your understanding on how array key's work. The following method would be far more appropriate: <?php // Create 00 to 23 in the array // 00 will store hits between midnight and 1am, // 01 for 1am - 2am ... // 23 for 11pm - midnight $timespread = array(); for ($cnt=0; $cnt<24; $cnt++) { $timespread[str_pad($cnt,2,'0',STR_PAD_LEFT)] = 0; } /* here you must work out the hour, and round *DOWN* (truncate off the minutes .. eg: for the current time */ $timespread[date("H")]++; ?> follow or have I lost ya? -- Dan Hardiker [dhardiker@staff.firstcreative.net] ADAM Software & Systems Engineer

« previous php.general (#97888) next »