Jump to content

Get position in array of certain value


AdRock

Recommended Posts

I have this array and I need to search it for the 'id' for a specific value $id.  What i'm trying to do is get the position in the array of the id i'm searching for.

 

I have tried 

$position = array_search($id, $rows);

but I get bool false

 

The contents of $rows (taken from MySQL database) is

array( {
[0]=>
array(1) {
["id"]=>
string(2) "24"
}
[1]=>
array(1) {
["id"]=>
string(2) "22"
}
[2]=>
array(1) {
["id"]=>
string(2) "12"
}
[3]=>
array(1) {
["id"]=>
string(2) "11"
}
[4]=>
array(1) {
["id"]=>
string(1) "8"
}
[5]=>
array(1) {
["id"]=>
string(1) "6"
}
[6]=>
array(1) {
["id"]=>
string(1) "5"
}
[7]=>
array(1) {
["id"]=>
string(1) "4"
}
}
Link to comment
https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/
Share on other sites

I have converted the var_dump you provided to an array and the code you are using is returning index 7 for the id matches 4

$rows = array(
  array("id" => "24"),
  array("id" => "22"),
  array("id" => "12"),
  array("id" => "11"),
  array("id" => "8"),
  array("id" => "6"),
  array("id" => "5"),
  array("id" => "4"),
);

$id = 4;

foreach($rows as $index => $row) {
    if($row['id'] == $id) {
        $position = $index;
        break;
    }
}

echo $position;

What is it you are trying to do?

 

If you need to get specific record, why not just query the database for which record matches id of 4?

An alternative to using FetchAll()  is Fetch().  You can build your own $rows array with customized indices...

 

$rows = array();

while($row = $stmt->fetch_assoc()){

    $id = $row['id'];         # store id column in variable if we're going to be unsetting it

    unset($row['id']);       # unset id if you don't want it to be part of the array, we can do this since we'll have the indices set to the ID

    $rows[$id] = $row;    # add row array to the rows array

}

 


If you need to get specific record, why not just query the database for which record matches id of 4?

 

+234

 

The database can search for records *much* faster than PHP can.

 

 

 


 unset id if you don't want it to be part of the array, we can do this since we'll have the indices set to the ID

 

The ID is part of the record and should never be separated from it. If you remove it and you decide to sort the array, chances are that the key's will point to different rows and you're done.

The ID is part of the record and should never be separated from it. If you remove it and you decide to sort the array, chances are that the key's will point to different rows and you're done.

 

The index is part of the record, and it's called asort().  

Try using a plain for loop and capture the increment value?

 

function arrayIndex( $arr, $elem ) {
if(gettype($arr)!=="array") return;
if(count($arr)==0) return -1;
for($i = 0; $i < count($arr); $i++) {
if($arr[$i]==$elem) return $i;
}
return -1;
The code doesn't do any recursive scanning, though.

I'll need to find a workaround for that.

 


And, you've forgotten SELECT statements have an ORDER BY clause. So, what's your point? :]

 

I haven't forgotten anything, I've thought about what the actual problem is and the solution does not require an array, a loop, not even an ORDER BY clause.

 

All the OP wants is to know how many records come before a particular record. That's one single query that everybody here should be able to work out.

 

Actually, this problem is almost identical to this one: http://forums.phpfreaks.com/topic/283517-php-associative-array/?do=findComment&comment=1456585

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.