Reply to Re: function that returns current numeric index?

Your name:

Reply:


Posted by jerrygarciuh on 10/30/05 11:18

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:
>
>> $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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация