function that returns current numeric index?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerrygarciuh

    function that returns current numeric index?

    Hello,

    In reading through the list of array-related functions I did not find on
    which returns the numeric index where the array's internal pointer currently
    sits. Is there such a function?

    TIA

    jg


  • Andy Hassall

    #2
    Re: function that returns current numeric index?

    On Thu, 9 Jun 2005 15:47:19 -0500, "jerrygarci uh"
    <designs@no.spa m.nolaflash.com > wrote:
    [color=blue]
    >In reading through the list of array-related functions I did not find on
    >which returns the numeric index where the array's internal pointer currently
    >sits. Is there such a function?[/color]



    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • jerrygarciuh

      #3
      Re: function that returns current numeric index?

      Andy,

      Thanks for the reply. Am I using this incorrectly?

      $ary = array('a','b',' c','d');
      while ($x = array_shift($ar y)) {
      echo key($ary) . ',';
      }

      // output is 0,0,0

      $ary = array('a' => 'apple','b' => 'bear','c' => 'cat','d' => 'dog');
      while ($a = array_shift($ar y)) {
      echo key($ary) . ',';
      }

      // output is b,c,d

      It seems to only work (as advertised in docs) on associative arrays.

      TIA for any help,

      jg





      "Andy Hassall" <andy@andyh.co. uk> wrote in message
      news:h0bha11cj8 tn5c7gg43tvska8 2ul7fl4p8@4ax.c om...[color=blue]
      > On Thu, 9 Jun 2005 15:47:19 -0500, "jerrygarci uh"
      > <designs@no.spa m.nolaflash.com > wrote:
      >[color=green]
      >>In reading through the list of array-related functions I did not find on
      >>which returns the numeric index where the array's internal pointer
      >>currently
      >>sits. Is there such a function?[/color]
      >
      > http://uk.php.net/manual/en/function.key.php
      >
      > --
      > Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
      > <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool[/color]


      Comment

      • Andy Hassall

        #4
        Re: function that returns current numeric index?

        On Thu, 9 Jun 2005 16:54:45 -0500, "jerrygarci uh"
        <designs@no.spa m.nolaflash.com > wrote:
        [color=blue]
        > $ary = array('a','b',' c','d');
        > while ($x = array_shift($ar y)) {
        > echo key($ary) . ',';
        > }
        >
        >// output is 0,0,0
        >
        >It seems to only work (as advertised in docs) on associative arrays.[/color]

        The output is consistent, since array_shift modifies the array.

        Each time you call it, it removes an entry __and shifts the numeric indices__.

        For associative arrays, the keys remain the same.

        --
        Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
        <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

        Comment

        • jerrygarciuh

          #5
          Re: function that returns current numeric index?

          Gotcha. Thing is, I am stumped as to how to use this in context.

          For instance if I am using foreach() which I know uses a copy of the array
          and my PHP version does not allow me to pass the array by ref then I see the
          pointer listed at 0 as I iterate:

          <?
          $ary = array('a','b',' c','d');
          foreach ($ary as $a) {
          echo key($ary) . ',';
          }

          ?>

          // outputs 0,0,0,0

          What I was looking to do was to be able to, at any given moment, was say
          'what is my index in array X?' but key() does not seem equipped to do this.

          Obviously I can do something like this :

          <?
          $ary = array('a','b',' c','d');
          $idx = 0;
          foreach ($ary as $a) {
          // whatever
          $idx++;
          }
          ?>

          but it's less elegant than I wanted.

          Thx for your replies!

          jg

          "Andy Hassall" <andy@andyh.co. uk> wrote in message
          news:l4fha1dsqs to68caaelo5vo9o 1c1hjeg4c@4ax.c om...[color=blue]
          > On Thu, 9 Jun 2005 16:54:45 -0500, "jerrygarci uh"
          > <designs@no.spa m.nolaflash.com > wrote:
          >[color=green]
          >> $ary = array('a','b',' c','d');
          >> while ($x = array_shift($ar y)) {
          >> echo key($ary) . ',';
          >> }
          >>
          >>// output is 0,0,0
          >>
          >>It seems to only work (as advertised in docs) on associative arrays.[/color]
          >
          > The output is consistent, since array_shift modifies the array.
          >
          > Each time you call it, it removes an entry __and shifts the numeric
          > indices__.
          >
          > For associative arrays, the keys remain the same.
          >
          > --
          > Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
          > <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool[/color]


          Comment

          • Geoff Berrow

            #6
            Re: function that returns current numeric index?

            I noticed that Message-ID: <8i5qe.41581$Fv .16352@lakeread 01> from
            jerrygarciuh contained the following:
            [color=blue]
            > $ary = array('a','b',' c','d');
            > foreach ($ary as $a) {
            > echo key($ary) . ',';
            > }[/color]

            $ary = array('a','b',' c','d');
            foreach ($ary as $key=>$a) {
            echo $key . ',';
            }

            //Should output 0,1,2,3,
            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • jerrygarciuh

              #7
              Re: function that returns current numeric index?

              Thanks!
              jg


              "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
              news:h8fia119le vblv5ita25207p8 78jr77cqq@4ax.c om...[color=blue]
              >I noticed that Message-ID: <8i5qe.41581$Fv .16352@lakeread 01> from
              > jerrygarciuh contained the following:
              >[color=green]
              >> $ary = array('a','b',' c','d');
              >> foreach ($ary as $a) {
              >> echo key($ary) . ',';
              >> }[/color]
              >
              > $ary = array('a','b',' c','d');
              > foreach ($ary as $key=>$a) {
              > echo $key . ',';
              > }
              >
              > //Should output 0,1,2,3,
              > --
              > Geoff Berrow (put thecat out to email)
              > It's only Usenet, no one dies.
              > My opinions, not the committee's, mine.
              > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


              Comment

              Working...