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
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?

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

 


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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 


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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.