Re: Array difficulty
| From: | Robin Vickery | Date: | Tue, 31 Jul 2007 15:13:57 +0000 |
| Subject: | Re: Array difficulty | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-259919@lists.php.net to get a copy of this message | ||
On 31/07/07, Alister Bulman <abulman@gmail.com> wrote:
> On 31/07/07, Carlton Whitehead <carlton.whitehead@cebesius.com> wrote:
> >> Couldn't you just do
> >> arsort($chance);
> >> $lastItem = chance[( count( $chance ) - 1 )];
>
> > I tried that earlier, but the problem is:
> > count( $chance ) - 1 ); returns an integer, so I would be asking for something like
> > $chance[1] or $chance[0], neither of which exist in the array. Keep in mind $chance only has keys
> > with string names:
>
> http://uk3.php.net/current
>
> $chance = array("lowercase" => 27, "uppercase" => 62,
> "integer" => 46);
> arsort($chance);
max() returns the maximum value of an array.
array_search() finds the key for a value
So all that's needed to find the key of the maximum value is:
$result = array_search(max($chance), $chance);
I'd pretty much guarantee it'll be about an order of magnitude faster
than any solution that relies on sorting the array.
-robin