For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > June 2005 > function that returns current numeric index?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author function that returns current numeric index?
jerrygarciuh

2005-06-09, 8:56 pm

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

2005-06-09, 8:56 pm

On Thu, 9 Jun 2005 15:47:19 -0500, "jerrygarciuh"
<designs@no.spam.nolaflash.com> wrote:

>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?


http://uk.php.net/manual/en/function.key.php

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

2005-06-09, 8:56 pm

Andy,

Thanks for the reply. Am I using this incorrectly?

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

// output is 0,0,0

$ary = array('a' => 'apple','b' => 'bear','c' => 'cat','d' => 'dog');
while ($a = array_shift($ary)) {
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:h0bha11cj8tn5c7gg43tvska82ul7fl4p8@
4ax.com...
> On Thu, 9 Jun 2005 15:47:19 -0500, "jerrygarciuh"
> <designs@no.spam.nolaflash.com> wrote:
>
>
> http://uk.php.net/manual/en/function.key.php
>
> --
> Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool



Andy Hassall

2005-06-09, 8:56 pm

On Thu, 9 Jun 2005 16:54:45 -0500, "jerrygarciuh"
<designs@no.spam.nolaflash.com> wrote:

> $ary = array('a','b','c','d');
> while ($x = array_shift($ary)) {
> echo key($ary) . ',';
> }
>
>// output is 0,0,0
>
>It seems to only work (as advertised in docs) on associative arrays.


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.andyhsoftware.co.uk/space> Space: disk usage analysis tool
jerrygarciuh

2005-06-10, 3:57 am

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:l4fha1dsqsto68caaelo5vo9o1c1hjeg4c@
4ax.com...
> On Thu, 9 Jun 2005 16:54:45 -0500, "jerrygarciuh"
> <designs@no.spam.nolaflash.com> wrote:
>
>
> 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.andyhsoftware.co.uk/space> Space: disk usage analysis tool



Geoff Berrow

2005-06-10, 8:55 am

I noticed that Message-ID: <8i5qe.41581$Fv.16352@lakeread01> from
jerrygarciuh contained the following:

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


$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/
jerrygarciuh

2005-06-10, 8:55 pm

Thanks!
jg


"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:h8fia119levblv5ita25207p878jr77cqq@
4ax.com...
>I noticed that Message-ID: <8i5qe.41581$Fv.16352@lakeread01> from
> jerrygarciuh contained the following:
>
>
> $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/



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com