Jump to content

[SOLVED] finding array position of max($var)?


Fog Juice

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

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