How do I find the key of data in an array returned from a database?

Let’s say I return 100 results from my database. I have 100 id’s returned to me as an array numbered 0-99.
I want to find the position, or key, of a current ID (in this case, $id) [php]<?php $id = $_GET['id']; ?>[/php] $id in the returned query [php]$row = mysql_fetch_array($result)l;[/php].

If you are looking for the position of a certain ID why not iterate through the array and count each element until you find it like

[php]
$index = 0;
while(($array[$index] != null) || ($array[$index] != $id))
{

     $position = $index;
     $index++

}
[/php]

Position would then be where that id is located

I believe you are after the array_search function.

[php]$key = array_search($id,$array);//$key will contain the key for the element of the array that has a value of $id, or boolean false if $id is not found[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service