473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Start array key at 1

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 "<tr>\n";
$day = array('Mon', 'Tue', 'Wed','Thu','Fri');
echo "\t<td class=\"tbl_day\">".$day[$weekdays]."</td>\n";

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

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

Cheers,

Chris

Oct 29 '07 #1
3 9868

"Chris" <ma*********@googlemail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.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 "<tr>\n";
$day = array('Mon', 'Tue', 'Wed','Thu','Fri');
echo "\t<td class=\"tbl_day\">".$day[$weekdays]."</td>\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
Oct 29 '07 #2
Shelly wrote:
"Chris" <ma*********@googlemail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.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 "<tr>\n";
$day = array('Mon', 'Tue', 'Wed','Thu','Fri');
echo "\t<td class=\"tbl_day\">".$day[$weekdays]."</td>\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
Oct 30 '07 #3
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 = $weekdays*$num_periods+$periods;
for($periods = 0; $periods < $num_periods; $periods++) {
$eachperiod = $weekdays * $num_periods + $periods + 1;
}

Oct 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Neil Morris | last post by:
Hi I have the following code that lists first names of people who's surnames are 'Morris'. What I am trying to do is search for first names that don't start at the beginning of the line ie. have...
1
by: Blankdraw | last post by:
I thought I cut out for myself a "simple" 1-module ANSI C programming task. I will in fact require extensive use of step-thru debugging. My old MS Visual C Developer Studio 4.2 should do this,...
4
by: Gidi | last post by:
Hi, i have a byte array, and i'm writing it to a file, i want to start a new line, how can i do it? (like WriteLine in StreamWriter) I'm using FileStream: FileStream fs = new...
11
by: Bob Nelson | last post by:
It's been a long time since I've posed a query here on c.l.c. My work environment evolved to primarily C++ and Perl with very little C, so I've forgotten quite a lot over time. This revisits the...
1
by: Chris | last post by:
Hello, I have a Windows Forms application which has buttons to click to run other programs. One of the applications it must run is a ClickOnce Windows Forms application, so the Application...
2
by: jonathan184 | last post by:
How to exclude lines from a txt file that start with a # Hi basically my script works fine. I wanted to add some comments in my config file but it is reading in the comment lines to. So...
4
by: JuAn2226 | last post by:
can anyone help me to to do coding for reset and start again. i got 10 array of random number, 10 array for start time how do i reset the both array after the 10 array is filled and start filling up...
18
by: dhtml | last post by:
Array.splice({}) What should it do? I think it should return a new Array with length 0. Array.splice(arr, start, deleteCount ]]) http://bclary.com/2004/11/07/#a-15.4.4.12 Example:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.