yami007 Posted April 10, 2012 Share Posted April 10, 2012 I want to store all the hours 0,1,2,3... in my $time array but it returns the last value only and I dont understand why... <?php $time = array(); $hour = 0; for ( $i=0; $i<=7; $i++ ) { $time['hour'] = $hour; $hour++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260675-problem-with-array-key/ Share on other sites More sharing options...
Adam Posted April 10, 2012 Share Posted April 10, 2012 In this line: $time['hour'] = $hour; You're overwriting the "hour" index in the array on every iteration. I'm guessing you either just want: $time[] = $hour; To have a single dimension array holding the times, or: $time['hour'][] = $hour; If you want the hour index to be an array itself. Quote Link to comment https://forums.phpfreaks.com/topic/260675-problem-with-array-key/#findComment-1336027 Share on other sites More sharing options...
yami007 Posted April 10, 2012 Author Share Posted April 10, 2012 You are right I wanted to do something like the latter...but I have minutes too, I want the array to be something like this Array ( [0] => Array ( [hour] => 8 [minute] => 0 ) [1] => Array ( [hour] => 8 [minute] => 45 ) ) anyway to do this ? Quote Link to comment https://forums.phpfreaks.com/topic/260675-problem-with-array-key/#findComment-1336030 Share on other sites More sharing options...
Adam Posted April 10, 2012 Share Posted April 10, 2012 You can set both at once like: $time[] = array( 'hour' => $hour, 'minute' => $minute ); But it kind of depends where you're getting the data from. Using a loop like you are doing is good for the hour, but you then need the minutes. Are you just wanting to generate an array for each hour with 0, 15, 30 and 45 minutes? Quote Link to comment https://forums.phpfreaks.com/topic/260675-problem-with-array-key/#findComment-1336036 Share on other sites More sharing options...
yami007 Posted April 10, 2012 Author Share Posted April 10, 2012 I get it, thank you dont worry about the minutes Quote Link to comment https://forums.phpfreaks.com/topic/260675-problem-with-array-key/#findComment-1336134 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.