Reputation: 299
Suppose you have the following array values assigned to a variable,
$erz = Array (
[0] => stdClass Object ( [id] => 43 [gt] => 112.5 )
[1] => stdClass Object ( [id] => 47 [gt] => 46 )
[2] => stdClass Object ( [id] => 48 [gt] => 23.75 )
[3] => stdClass Object ( [id] => 49 [gt] => 12.5 )
)
I need to be able to get the array index number given the id. So for instance I want to get 2 given id 48, or get 3 given id 49, etc. Is there a php command able to do this?
Upvotes: 2
Views: 3222
Reputation: 24344
I dont think there is but its easy to set up your own function..
function findArrayIndex($arr, $searchId) {
$arrLen = count($arr);
for($i=0; $i < $arrLen; $i++) {
if($arr[$i][id] == $searchId) return $i;
}
return -1;
}
Upvotes: 1
Reputation: 10084
No, there is no such funcion. There is an array_search()
actually, but you can't use it with objects. For example, here has been asked a simmilar question: PHP - find entry by object property from a array of objects
So you have to make your own loop:
$result = null;
$givenID = 43;
foreach ($erz as $key => $element)
{
if ($element->id == $givenID)
$result = $key;
}
Upvotes: 0