473,388 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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
Jul 17 '05 #1
6 1689
On Thu, 9 Jun 2005 15:47:19 -0500, "jerrygarciuh"
<de*****@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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
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" <an**@andyh.co.uk> wrote in message
news:h0********************************@4ax.com...
On Thu, 9 Jun 2005 15:47:19 -0500, "jerrygarciuh"
<de*****@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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool

Jul 17 '05 #3
On Thu, 9 Jun 2005 16:54:45 -0500, "jerrygarciuh"
<de*****@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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4
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" <an**@andyh.co.uk> wrote in message
news:l4********************************@4ax.com...
On Thu, 9 Jun 2005 16:54:45 -0500, "jerrygarciuh"
<de*****@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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool

Jul 17 '05 #5
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/
Jul 17 '05 #6
Thanks!
jg
"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:h8********************************@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 $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/

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: Terry Coccoli | last post by:
Has anyone created a user function to return the ordinal position based on numeric input? In other words, f(1) returns 'st', f(2) returns 'nd', f(3) returns 'rd', etc.
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
8
by: Lucas Lemmens | last post by:
Dear pythonians, I've been reading/thinking about the famous function call speedup trick where you use a function in the local context to represent a "remoter" function to speed up the 'function...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
6
by: daveyand | last post by:
Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values
2
by: Wade | last post by:
I am pretty new to Access, especially writing code, but I found code on the web to do just what I want and it is posted below. It returns a field value from a previous record. I found the code at:...
3
by: kalar | last post by:
From a Book: we have struct node{ int data; struct node *next; } The question: Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.