AdRock Posted November 1, 2013 Share Posted November 1, 2013 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" } } Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/ Share on other sites More sharing options...
AdRock Posted November 1, 2013 Author Share Posted November 1, 2013 I solved it by using foreach($rows as $index => $row) { if($row['id'] == $id) $position = $index; } Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456479 Share on other sites More sharing options...
AdRock Posted November 1, 2013 Author Share Posted November 1, 2013 I just found it was returning the wrong index I am looking for an id of 4 which is index position 7 but it returns 4 instead foreach($rows as $index => $row) { if($row['id'] == $id) $page = $index+1; } var_dump($page); Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456480 Share on other sites More sharing options...
objnoob Posted November 1, 2013 Share Posted November 1, 2013 You could break out of the loop after finding it, foreach($rows as $index => $row) { if($row['id'] == $id){ $page = $index+1; break; # we found what we were searching for, break out of loop } } Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456481 Share on other sites More sharing options...
AdRock Posted November 1, 2013 Author Share Posted November 1, 2013 Still shows the same value Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456482 Share on other sites More sharing options...
Ch0cu3r Posted November 1, 2013 Share Posted November 1, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456483 Share on other sites More sharing options...
objnoob Posted November 1, 2013 Share Posted November 1, 2013 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 } Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456496 Share on other sites More sharing options...
vinny42 Posted November 1, 2013 Share Posted November 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456509 Share on other sites More sharing options...
objnoob Posted November 1, 2013 Share Posted November 1, 2013 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(). Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456516 Share on other sites More sharing options...
vinny42 Posted November 1, 2013 Share Posted November 1, 2013 The index is part of the record, and it's called asort(). Don't be a smartass when you're making a classic beginners mistake. Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456519 Share on other sites More sharing options...
Irate Posted November 1, 2013 Share Posted November 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456556 Share on other sites More sharing options...
vinny42 Posted November 2, 2013 Share Posted November 2, 2013 The code doesn't do any recursive scanning, though.I'll need to find a workaround for that. You may want to read the original question; he's not looking far a value "anywhere in the array", he's forgotten that queries have a WHERE clause. :-) Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456574 Share on other sites More sharing options...
Irate Posted November 2, 2013 Share Posted November 2, 2013 Uh, yeah, that's true... Well, nevermind me, then... Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456613 Share on other sites More sharing options...
objnoob Posted November 3, 2013 Share Posted November 3, 2013 You may want to read the original question; he's not looking far a value "anywhere in the array", he's forgotten that queries have a WHERE clause. :-) And, you've forgotten SELECT statements have an ORDER BY clause. So, what's your point? :] Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456650 Share on other sites More sharing options...
objnoob Posted November 3, 2013 Share Posted November 3, 2013 Still shows the same value Yes, it will show the same value. The idea of breaking out of loop, is to stop iterating through the remaining array elements. Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456651 Share on other sites More sharing options...
vinny42 Posted November 3, 2013 Share Posted November 3, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/283502-get-position-in-array-of-certain-value/#findComment-1456675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.