Start array key at 1
am 29.10.2007 22:00:42 von ChrisHow can I change the following snippet to have the array key start at
1 and not 0?
$timetable = explode(',', $row['timetable']);
It is taken from the following code...first time around the loop
$timetable["$eachperiod"] = $timetable[1] which puts me out of sync
with the number of fields I am creating ie. I lose $timetable[0].
// Weekdays
for ($weekdays=0;$weekdays <=4;$weekdays++) {
echo "
$day = array('Mon', 'Tue', 'Wed','Thu','Fri');
echo "\t
// Periods
for ($periods=1;$periods <=$num_periods;$periods++) {
$eachperiod = $weekdays*$num_periods+$periods;
// Fill the table with periods
$fields = $timetable["$eachperiod"];
Cheers,
Chris
Re: Start array key at 1
am 29.10.2007 23:41:38 von Shelly"Chris" Shelly wrote: Chris schrieb:
news:1193691642.582876.300860@22g2000hsm.googlegroups.com...
> How can I change the following snippet to have the array key start at
> 1 and not 0?
>
> $timetable = explode(',', $row['timetable']);
>
> It is taken from the following code...first time around the loop
> $timetable["$eachperiod"] = $timetable[1] which puts me out of sync
> with the number of fields I am creating ie. I lose $timetable[0].
>
> // Weekdays
> for ($weekdays=0;$weekdays <=4;$weekdays++) {
> echo "\n";
> $day = array('Mon', 'Tue', 'Wed','Thu','Fri');
> echo "\t".$day[$weekdays]." \n";
>
> // Periods
> for ($periods=1;$periods <=$num_periods;$periods++) {
> $eachperiod = $weekdays*$num_periods+$periods;
>
> // Fill the table with periods
> $fields = $timetable["$eachperiod"];
>
> Cheers,
>
> Chris
What is your problem? Except for ancient languages like Fortran, arrays
start at 0. Simply rewrite the loop to be:
for ($periods=0;$periods <$num_periods;$periods++) {
Shelly
Re: Start array key at 1
am 30.10.2007 10:20:42 von Robin Goodall
> "Chris"
> news:1193691642.582876.300860@22g2000hsm.googlegroups.com...
>> How can I change the following snippet to have the array key start at
>> 1 and not 0?
>>
>> $timetable = explode(',', $row['timetable']);
>>
>> It is taken from the following code...first time around the loop
>> $timetable["$eachperiod"] = $timetable[1] which puts me out of sync
>> with the number of fields I am creating ie. I lose $timetable[0].
>>
>> // Weekdays
>> for ($weekdays=0;$weekdays <=4;$weekdays++) {
>> echo "\n";
>> $day = array('Mon', 'Tue', 'Wed','Thu','Fri');
>> echo "\t".$day[$weekdays]." \n";
>>
>> // Periods
>> for ($periods=1;$periods <=$num_periods;$periods++) {
>> $eachperiod = $weekdays*$num_periods+$periods;
>>
>> // Fill the table with periods
>> $fields = $timetable["$eachperiod"];
>>
>> Cheers,
>>
>> Chris
>
> What is your problem? Except for ancient languages like Fortran, arrays
> start at 0. Simply rewrite the loop to be:
>
> for ($periods=0;$periods <$num_periods;$periods++) {
>
> Shelly
>
>
Also you don't need the double quotes around $eachperiod, i.e.
$fields=$timetable[$eachperiod];
Robin
Re: Start array key at 1
am 30.10.2007 15:28:56 von dafox
> How can I change the following snippet to have the array key start at
> 1 and not 0?
You could unshift the array and then delete the new item immediately,
but this ugly and makes no sense, since you can work with your array
without transformation.
array_unshift($periods);
unset($periods[0]);
[...]
> // Periods
> for ($periods=1;$periods <=$num_periods;$periods++) {
> $eachperiod = $weekdays*$num_periods+$periods;
for($periods = 0; $periods < $num_periods; $periods++) {
$eachperiod = $weekdays * $num_periods + $periods + 1;
}