For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > October 2007 > Start array key at 1









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Start array key at 1
Chris

2007-10-29, 7:02 pm

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].

// Wdays
for ($wdays=0;$wdays <=4;$wdays++) {
echo "<tr>\n";
$day = array('Mon', 'Tue', 'Wed','Thu','Fri');
echo "\t<td class=\"tbl_day\">".$day[$wdays]."</td>\n";

// Periods
for ($periods=1;$periods <=$num_periods;$periods++) {
$eachperiod = $wdays*$num_periods+$periods;

// Fill the table with periods
$fields = $timetable["$eachperiod"];

Cheers,

Chris

Shelly

2007-10-29, 7:02 pm


"Chris" <matchett123@googlemail.com> wrote in message
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].
>
> // Wdays
> for ($wdays=0;$wdays <=4;$wdays++) {
> echo "<tr>\n";
> $day = array('Mon', 'Tue', 'Wed','Thu','Fri');
> echo "\t<td class=\"tbl_day\">".$day[$wdays]."</td>\n";
>
> // Periods
> for ($periods=1;$periods <=$num_periods;$periods++) {
> $eachperiod = $wdays*$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


Robin

2007-10-30, 8:01 am

Shelly wrote:
> "Chris" <matchett123@googlemail.com> wrote in message
> news:1193691642.582876.300860@22g2000hsm.googlegroups.com...
>
> 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
Thomas Hamacher

2007-10-30, 7:02 pm

Chris schrieb:
> 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 = $wdays*$num_periods+$periods;


for($periods = 0; $periods < $num_periods; $periods++) {
$eachperiod = $wdays * $num_periods + $periods + 1;
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com