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

0 Members and 1 Guest are viewing this topic.

Offline Fog JuiceTopic starter

  • Enthusiast
  • Posts: 93
    • 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

  • Addict
  • Posts: 2,645
  • Gender: Male
  • Programming, the art of combining math and logic.
    • View Profile
    • Crikey Games Pty Ltd
Re: finding array position of max($var)?
« Reply #1 on: June 26, 2009, 01:56:43 AM »
Why is hashing a hash bad practice?
Quote from: Zane
A business person’s best customers are always using the cheapest piece of shit computer out there. I don’t have the sources to back it up, but I’d like to say it’s a proven fact.
Quote from: requinix
Use objects when they make sense and functions when they don't.
Crikey Games Pty Ltd | Realm Battles Classic
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 Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,751
    • 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
  • Posts: 93
    • 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 »