2

Possible Duplicate:
php - get numeric index of associative array

$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
   //echo $position ( 1,2 )
}

Can I get the position in the array with a simple function ?

Community
  • 1
  • 1
johnlemon
  • 20,761
  • 42
  • 119
  • 178

2 Answers2

1

Try:

$i = 0;
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
   $i++;
   echo $i;
}
christopher_b
  • 958
  • 5
  • 13
  • 2
    You can skip one line of code by using `echo ++$i;`. See [Incrementing/Decrementing Operators](http://php.net/manual/en/language.operators.increment.php) for more information. – Treffynnon Jul 26 '11 at 14:54
0
$array = array('a'=>'a', 'b'=>'b');
for ($x = 0; $x < count($array);$x++)
        echo $x."<br >";
herchila
  • 1
  • 1