Author Topic: [SOLVED] finding array position of max($var)?  (Read 191 times)

0 Members and 1 Guest are viewing this topic.

Offline Fog JuiceTopic starter

  • Enthusiast
    • View Profile
[SOLVED] finding array position of max($var)?
« on: June 26, 2009, 01:17:03 AM »
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.

Offline MasterACE14

  • Supporter
  • Addict
  • *
  • Gender: Male
  • Reverse your World.
    • View Profile
    • Crikey Games Pty Ltd
Re: finding array position of max($var)?
« Reply #1 on: June 26, 2009, 01:56:43 AM »
Crikey Games Pty Ltd | Realm Battles | Conflicting Forces in development...

It's too big a world to be in competition with everyone.  The only person who I have to be better than is myself. ~Harry Morgan

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: finding array position of max($var)?
« Reply #2 on: June 26, 2009, 02:06:16 AM »
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
Code: [Select]
$index = array_search($array, max($array));
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline Fog JuiceTopic starter

  • Enthusiast
    • View Profile
Re: finding array position of max($var)?
« Reply #3 on: June 26, 2009, 02:59:07 AM »
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
Code: [Select]
$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.
« Last Edit: June 26, 2009, 02:59:47 AM by Fog Juice »

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.