You should use the key() function.
key($array)
should return the current key.
If you need the position of the current key:
array_search($key, array_keys($array));
Answer from Zahymaka on Stack OverflowYou should use the key() function.
key($array)
should return the current key.
If you need the position of the current key:
array_search($key, array_keys($array));
PHP arrays are both integer-indexed and string-indexed. You can even mix them:
array('red', 'green', 'white', 'color3'=>'blue', 3=>'yellow');
What do you want the index to be for the value 'blue'? Is it 3? But that's actually the index of the value 'yellow', so that would be an ambiguity.
Another solution for you is to coerce the array to an integer-indexed list of values.
foreach (array_values($array) as
value) {
echo "
value\n";
}
Output:
0: red
1: green
2: white
3: blue
4: yellow
Sounds like you are in search of array_keys (to get key by index) and array_values (to get value by index) functions:
$array = array("BTC_XRP" => 1, "EUR_XRP" => 234, "USD_XRP" => 567);
$keys = array_keys( $array );
$values = array_values( $array );
var_dump( $keys[1] ); // string(7) "EUR_XRP"
var_dump( $values[1] ); // int(234)
Or use a foreach as Joshua suggests.
This question has a confusing title. What the OP needed is simply how to iterate over associative array. And the answer is just as silly:
foreach ($array as $key => $value) {
echo "Key: $key, value: $value";
}
But the title, that attracts people from Google search, explicitly asks how to get a certain value from associative array by its position (or "index"). For this you need a function called array_slice():
$array = array('a'=>'apple', 'b'=>'banana', '42'=>'pear', 'd'=>'orange');
$index = 2;
$elem = array_slice($array, $index, 1, true);
$key = key($elem);
$value = reset($elem);
echo "Key: $key, value: $value"; // Key: 42, value: pear
$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
$array = array_values($array);
echo $array[0]; //bar
echo $array[1]; //bin
echo $array[2]; //ipsum
array_values() will do pretty much what you want:
$numeric_indexed_array = array_values($your_array);
// $numeric_indexed_array = array('bar', 'bin', 'ipsum');
print($numeric_indexed_array[0]); // bar
Try the array_search function.
From the first example in the manual:
<?php $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $array); // $key = 2; $key = array_search('red', $array); // $key = 1; ?>
A word of caution
When comparing the result, make sure to test explicitly for the value false using the === operator.
Because arrays in PHP are 0-based, if the element you're searching for is the first element in the array, a value of 0 will be returned.
While 0 is a valid result, it's also a falsy value, meaning the following will fail:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('blue',$array);
if($key == false) {
throw new Exception('Element not found');
}
?>
This is because the == operator checks for equality (by type-juggling), while the === operator checks for identity.
have in mind that, if you think that your search item can be found more than once, you should use array_keys() because it will return keys for all matching values, not only the first matching key as array_search().
Regards.
array_search will do what you are looking for
Taken straight from the PHP manual:
array_search() - Searches the array for a given value and returns the corresponding key if successful
If you're searching a non-associative array, it returns the corresponding key, which is the index you're looking for. For non-consecutively indexed arrays (i.e. array(1 => 'Foo', 3 => 'Bar', ...)) you can use the result of array_values() and search in it.