Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Locating the index of an array

Status
Not open for further replies.

moham

Programmer
Nov 12, 2001
13
0
0
US
How do i locate the index of an array?

for example if this is my php page:

<?php
$ArrayVar = (&quot;a&quot;, &quot;b&quot;, &quot;c&quot;); //this is the array
foreach ($ArrayVar)
{
echo(indexof($ArrayVar)); //need php equiv of &quot;indexof&quot;
}
?>

I want to see the following output:
0
1
2
 
Here is one way:

<?
$ArrayVar = array(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;);
while (list ($key, $val) = each ($ArrayVar)) {
echo &quot;$key<br>&quot;;
}
?>
 
Also found this code in the PHP docs ... dont know if it is better or not (I have never used the foreach in PHP)

<?
$ArrayVar = array(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;);
foreach ($ArrayVar as $key => $value) {
echo &quot;Key: $key; Value: $value<br>\n&quot;;
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top