petenaylor Posted December 3, 2012 Share Posted December 3, 2012 Hi all I have a function below that searches an array and assigns it to $totalmonthlycost. I need to know how I get the array key of the chosen array value ($m12 - $m60) How do I return the array key in the below code? $monthvalues = array($m12,$m18,$m24,$m30,$m36,$m42,$m48,$m54,$m60); function closest($monthvalues, $number){ #does the array already contain the number? if($i = array_search( $number, $monthvalues)) return $i; #add the number to the array $monthvalues[] = $number; #sort and refind the number sort($monthvalues); $i = array_search($number, $monthvalues); #check if there is a number above it if($i && isset($monthvalues[$i-1])) return $monthvalues[$i-1]; //alternatively you could return the number itself here, or below it depending on your requirements } $totalmonthlycost = closest($monthvalues, $idealcostpermonth); Thanks for your help. Pete Quote Link to comment https://forums.phpfreaks.com/topic/271537-find-array-key/ Share on other sites More sharing options...
Muddy_Funster Posted December 3, 2012 Share Posted December 3, 2012 I don't get your question - array_search(), which you are already using, returns the key if it finds a match, which is what you seem to be asking how to do. I'm confused. Quote Link to comment https://forums.phpfreaks.com/topic/271537-find-array-key/#findComment-1397198 Share on other sites More sharing options...
petenaylor Posted December 3, 2012 Author Share Posted December 3, 2012 Hi there Yes that's correct I just need to echo out the array key itself. Quote Link to comment https://forums.phpfreaks.com/topic/271537-find-array-key/#findComment-1397200 Share on other sites More sharing options...
Muddy_Funster Posted December 3, 2012 Share Posted December 3, 2012 so if(array_search($array)){ $value = array_search($array); } else{ $value = "value not found"; } echo $value; Quote Link to comment https://forums.phpfreaks.com/topic/271537-find-array-key/#findComment-1397202 Share on other sites More sharing options...
petenaylor Posted December 3, 2012 Author Share Posted December 3, 2012 Thanks, can you help with the function I posted? I need it to output the array key as well as the value. for instance, if it chose $m18 then I want to to show $m18. Quote Link to comment https://forums.phpfreaks.com/topic/271537-find-array-key/#findComment-1397205 Share on other sites More sharing options...
Jessica Posted December 3, 2012 Share Posted December 3, 2012 You're not currently echo'ing anything. If you have the key just echo it. Quote Link to comment https://forums.phpfreaks.com/topic/271537-find-array-key/#findComment-1397207 Share on other sites More sharing options...
Muddy_Funster Posted December 3, 2012 Share Posted December 3, 2012 you could try changing your returns to return array($i, $monthvalues); then just call the values from the function : $values = closest($monthvalues, $number); echo "{$values['1']} was found at position {$values['0']}"; or something along those lines should do it. Quote Link to comment https://forums.phpfreaks.com/topic/271537-find-array-key/#findComment-1397209 Share on other sites More sharing options...
petenaylor Posted December 3, 2012 Author Share Posted December 3, 2012 Sorted! Thanks for your help folks! Quote Link to comment https://forums.phpfreaks.com/topic/271537-find-array-key/#findComment-1397222 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.