Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6364

Re: Using count() as an array index

From Martin Leese <please@see.Web.for.e-mail.INVALID>
Newsgroups comp.lang.php
Subject Re: Using count() as an array index
Date 2012-06-18 08:46 -0600
Organization A noiseless patient Spider
Message-ID <jrnf04$8hg$1@dont-email.me> (permalink)
References (1 earlier) <jrgab8$n9j$1@dont-email.me> <jrihcn$7ht$1@dont-email.me> <jrik64$ovm$1@dont-email.me> <jrknos$cg2$1@dont-email.me> <jrkpgd$m1c$1@dont-email.me>

Show all headers | View raw


Jerry Stuckle wrote:
> 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.

No, I will not need to change my code.  I
am using an array as a replacement for a
database.  Here is a snippet of my file
faqData.php to give you the idea:

//  Set data to identify Web page, and set prev and next anchors
//  Note that the first set must have index explicitly set to [1]
     $tempIndex = 1;
     $dataHref[$tempIndex] = "Ambisonic/faq_section01.html";
     $dataAnchorText[$tempIndex] = "1. Where can I get this FAQ?";
     $dataCompleteText[$tempIndex] = "1. <a href=\"#SECTION1\">Where can 
I get this FAQ?</a>";
     $dataSplitText[$tempIndex] = "1. <a rel=\"section\" 
href=\"$dataHref[$tempIndex]\">Where can I get this FAQ?</a>";

     $tempIndex++;
     $dataHref[$tempIndex] = "Ambisonic/faq_section02.html";
     $dataAnchorText[$tempIndex] = "2. Corrections to the FAQ";
     $dataCompleteText[$tempIndex] = "2. <a 
href=\"#SECTION2\">Corrections to the FAQ</a>";
     $dataSplitText[$tempIndex] = "2. <a rel=\"section\" 
href=\"$dataHref[$tempIndex]\">Corrections to the FAQ</a>";

-- 
Regards,
Martin Leese
E-mail: please@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-15 12:51 -0600
  Re: Using count() as an array index Gregor Kofler <usenet@gregorkofler.com> - 2012-06-15 21:04 +0200
  Re: Using count() as an array index Denis McMahon <denismfmcmahon@gmail.com> - 2012-06-15 21:38 +0000
  Re: Using count() as an array index Chuck Anderson <cycletourist@invalid.invalid> - 2012-06-15 15:44 -0600
    Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-16 11:56 -0600
      Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-16 14:44 -0400
        Re: Using count() as an array index Scott Johnson <noonehome@chalupasworld.com> - 2012-06-16 14:43 -0700
        Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-17 07:57 -0600
          Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-17 10:27 -0400
            Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-18 08:46 -0600
              Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-18 13:38 -0400
                Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-19 09:03 -0600
                Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-19 11:27 -0400
        Re: Using count() as an array index Arno Welzel <usenet@arnowelzel.de> - 2012-06-18 09:46 +0200
          Re: Using count() as an array index Norman Peelman <npeelmandog@cfl.rr.com> - 2012-06-18 06:32 -0400
            Re: Using count() as an array index The Natural Philosopher <tnp@invalid.invalid> - 2012-06-18 12:24 +0100
            Re: Using count() as an array index Arno Welzel <usenet@arnowelzel.de> - 2012-06-18 19:43 +0200
              Re: Using count() as an array index "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-06-19 09:31 +0200
          Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-18 08:43 -0400
        Re: Using count() as an array index Christoph Becker <cmbecker69@gmx.de> - 2012-06-19 13:24 +0200
          Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-19 07:39 -0400
            Re: Using count() as an array index Christoph Becker <cmbecker69@gmx.de> - 2012-06-19 15:04 +0200
          Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-19 09:14 -0600

csiph-web