Re: Using count() as an array index [message #178430 is a reply to message #178429] |
Sun, 17 June 2012 14:27 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 6/17/2012 9:57 AM, Martin Leese wrote:
> Jerry Stuckle wrote:
> ...
>> Be aware, though - the typical array in PHP starts counting at 0, not
>> 1. So an array of 3 elements would be numbered 0, 1 and 2. In this
>> case, an element with an index of 3 (count($anArray)) is not set (and
>> will give a warning if you try to use it).
>>
>> You need to get used to this idea; it's used throughout PHP.
>
> The test code I posted was taken from my
> application in which I deliberately start my
> array with [1]. This is because the array
> holds numbered sections of an FAQ, and it
> makes more sense to have Section 1 in array
> element [1], etc.
>
Actually, I think it's a bad way to do things. What happens if you want
to add a new section - or delete one? You'll have to change your code,
which should never be the case.
A better way is to hold the information in a database.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|