Reputation: 21386
I have a PHP array, say,
$array = Array("Name1","Name2","Name3","Name4","Name5");
I want to find the position of these names in the array.
I want to return 0 for Name1
and 2 for Name3
.
How can I do this?
Upvotes: 0
Views: 143
Reputation: 263
Something like that:
<?php
$array = Array("Name1", "Name2", "Name3", "Name4", "Name5");
$searchValue = "Name1";
$keys = array_keys($array, $searchValue);
// test it
print_r($keys);
?>
Upvotes: 0
Reputation: 60516
$pos = array_search("Name3", $array);
Should be what you are looking for, note that to be safe, result checking should be using === (three equal signs) so that those returning 0 (if you are looking for thing in the first element) is also checked for type when you are comparing them in an if statement
if(array_search($name, $array) === 0)
Upvotes: 0
Reputation: 100175
Do you mean:
$key = array_search('Name1', $array);
Ref: array_search
Upvotes: 1