Fog Juice Posted June 26, 2009 Share Posted June 26, 2009 Is there a function or shortcut to find the position an array item is in when using the max() function? For example, if I have an array that goes $array = array(1,2,5,123,45,23) and I use max($array), it will return 123 which is in position 3 in the array, but is there a function to get that position? Thanks. Link to comment https://forums.phpfreaks.com/topic/163731-solved-finding-array-position-of-maxvar/ Share on other sites More sharing options...
MasterACE14 Posted June 26, 2009 Share Posted June 26, 2009 use strpos(); Link to comment https://forums.phpfreaks.com/topic/163731-solved-finding-array-position-of-maxvar/#findComment-863945 Share on other sites More sharing options...
Psycho Posted June 26, 2009 Share Posted June 26, 2009 use strpos(); Um, say what? The OP asked about finding the index for an item in an array. strpos() is, obviously, for strings. Try this $index = array_search($array, max($array)); Link to comment https://forums.phpfreaks.com/topic/163731-solved-finding-array-position-of-maxvar/#findComment-863949 Share on other sites More sharing options...
Fog Juice Posted June 26, 2009 Author Share Posted June 26, 2009 use strpos(); Um, say what? The OP asked about finding the index for an item in an array. strpos() is, obviously, for strings. Try this $index = array_search($array, max($array)); yes thank you, that is perfect. But I read in the php docs you have it backwards, array_search is array_search(needle, haystack). strpos is opposite where it is strpos(haystack, needle).. funny how they're opposite lol. But thanks a bunch, that's exactly what I needed. Link to comment https://forums.phpfreaks.com/topic/163731-solved-finding-array-position-of-maxvar/#findComment-863957 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.