Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

About array keys and numerical indexes 1

Status
Not open for further replies.

kaptlid

Technical User
Nov 26, 2006
86
US
I am aware of the fact that a numerical index on an array starts from the number zero. Is it possible for it to start from the number one? I did not notice any functions in the php manual for this.

Thanks,
 
Short answer...No. Next answer, have your program skip [0] and start at 1 when building the array. I have rarely found it necessary to do that. I usually just iterate using -1 if need be.

Mark
 
make an if statement,

for ($i = 1, $i < $total, $i++){
echo $array($i);
}

Grtz Bertjh
 
BeRtjh's statement will not work for creating the array, and for accessing the element you need to swap the round brackets for square ones in the echo line.

to create a 1-based array

Code:
$array[1] = 'sometext';
$array[] = 'sometext2';
$array[] = 'sometext3';
print_r ($sometext);
i.e. the implicit array element creation of $array[] just increments the highest numeric array key. if you have forced the start on a 1 rather than a zero then that's all you need to do.
if you are building your array dynamically from, say, a mysql query try this kind of thing

Code:
$queryresults[0]=null;
while ($row = mysql_query("some query") {
  $queryresults[] = $row;
}
unset($queryresults[0]);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top