gary00ie Posted April 26, 2012 Share Posted April 26, 2012 Hi, I need to do this: Write a method to quickly find the array index position of a given number in a ascending sorted array of numbers. e.g. in an array { 1, 2, 5, 10, 102 }, the array index position of a given number 5, would have an answer of 2. We're looking for an answer that's better than O(n). I was using php's built in methods but I need to do this with a basic function using no helper methods. Can anybody help me out with this? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/ Share on other sites More sharing options...
trq Posted April 26, 2012 Share Posted April 26, 2012 Can anybody help me out with this? Seems pretty straight forward, where exactly are you stuck? I was using php's built in methods but I need to do this with a basic function using no helper methods. Sorry, but that statement makes little sense. Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/#findComment-1340643 Share on other sites More sharing options...
gary00ie Posted April 26, 2012 Author Share Posted April 26, 2012 Can anybody help me out with this? Seems pretty straight forward, where exactly are you stuck? I was using php's built in methods but I need to do this with a basic function using no helper methods. Sorry, but that statement makes little sense. I was using the built in arryay_keys and array_search to do this. But, I need to do it without using those methods. Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/#findComment-1340649 Share on other sites More sharing options...
trq Posted April 26, 2012 Share Posted April 26, 2012 But, I need to do it without using those methods. Why? Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/#findComment-1340650 Share on other sites More sharing options...
gary00ie Posted April 26, 2012 Author Share Posted April 26, 2012 But, I need to do it without using those methods. Why? It is what I was asked for. I'm just having a little trouble figuring it out. Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/#findComment-1340651 Share on other sites More sharing options...
trq Posted April 26, 2012 Share Posted April 26, 2012 Whatever. Here is an example without any error handling written for PHP5.4. function findIndex($array, $value) { return array_reverse($array)[$value]; } Should give you the idea without handing you the code. Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/#findComment-1340653 Share on other sites More sharing options...
gary00ie Posted April 26, 2012 Author Share Posted April 26, 2012 Thank you for that. It is pretty much very close to what I had. I need to do it with no helper PHP functions though. Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/#findComment-1340656 Share on other sites More sharing options...
marcus Posted April 26, 2012 Share Posted April 26, 2012 function findIndex($array, $value){ if(count($array) > 0){ $i = 0; foreach($array AS $val){ // check if val is equal to value, if it is return $i // increment $i } } return -1; } Alternatively you could use a for loop $count = count($array); for($i = 0; $i < $count; $i++){ // if array position at $i equals your value, return $i } -1 is used to indicate the value was never found. So when using the function $index = findIndex($array, $value); if($index < 0){ // not found }else { // found at $index } Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/#findComment-1340666 Share on other sites More sharing options...
gary00ie Posted April 26, 2012 Author Share Posted April 26, 2012 Thanks for all the help guys. I think I have this figured out now. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/261634-php-arrays/#findComment-1340735 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.