Re: [PHP] Array difficulty

2007-08-07 Thread Richard Lynch
On Tue, July 31, 2007 8:27 am, Carlton Whitehead wrote:
> I have an array like this:
>
> $chance = array("lowercase" => 27, "uppercase" => 62, "integer" =>
> 46);
>
> The values for each of the keys are randomly generated. I want to find
> the key name of the one which has the highest value. Currently, I'm
> doing this as follows:
>
> arsort($chance);

$result = key($chance);

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array difficulty

2007-07-31 Thread Robin Vickery
On 31/07/07, Alister Bulman <[EMAIL PROTECTED]> wrote:
> On 31/07/07, Carlton Whitehead <[EMAIL PROTECTED]> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array difficulty

2007-07-31 Thread Alister Bulman
On 31/07/07, Carlton Whitehead <[EMAIL PROTECTED]> 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);
$lastItem = current($chance);
echo "$lastItem\n\n";   // 62

Alister

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Array difficulty

2007-07-31 Thread Chris Boget
> > Couldn't you just do
> > arsort($chance);
> > $lastItem = chance[( count( $chance ) - 1 )];
> $lastItem = end( $chance );

end() returns the value as well.  You would also need to use key().

thnx,
Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Array difficulty

2007-07-31 Thread Robert Cummings
On Tue, 2007-07-31 at 14:43 +0100, Chris Boget wrote:
> > At this point, $result would be equal to "uppercase".  I feel 
> > like this is a really kludgey way to accomplish this.  Is there 
> > a better way?
> 
> Couldn't you just do
> 
> arsort($chance);
> $lastItem = chance[( count( $chance ) - 1 )];

$lastItem = end( $chance );

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array difficulty

2007-07-31 Thread Robin Vickery
On 31/07/07, Carlton Whitehead <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have an array like this:
>
> $chance = array("lowercase" => 27, "uppercase" => 62, "integer" => 46);
>
> The values for each of the keys are randomly generated. I want to find the 
> key name of the one which has the highest value. Currently, I'm doing this as 
> follows:
>
> arsort($chance);
> foreach ($chance as $type => $value)
> {
> $result = $type;
> break;
> }
>

Assuming $chance is non-empty.

$result = array_search(max($chance), $chance);

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array difficulty

2007-07-31 Thread Chris Boget

At this point, $result would be equal to "uppercase".  I feel like
this is a really kludgey way to accomplish this.  Is there a better way?

Not tested it, but max() should work as the first parameter can be an
array:
http://uk3.php.net/max


Except he's looking for the key and not the value, which max() would return. 
Come to think of it, the solution I provided does the same thing.


Untested but one of the below should work:

arsort($chance);
$lastItem = key( $chance[( count( $chance ) - 1 )]);

OR

arsort($chance);
$lastItem = key( end( $chance ));

thnx,
Chris 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array difficulty

2007-07-31 Thread Carlton Whitehead
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:

The array looks like this:

$chance = array("lowercase" => 27, "uppercase" => 62, "integer" => 46);

The values assigned to each key are randomly generated.

Regards,
Carlton Whitehead

- Original Message -
From: "Chris Boget" <[EMAIL PROTECTED]>
To: "Carlton Whitehead" <[EMAIL PROTECTED]>, php-general@lists.php.net
Sent: Tuesday, July 31, 2007 9:43:00 AM (GMT-0500) America/New_York
Subject: RE: [PHP] Array difficulty

> At this point, $result would be equal to "uppercase".  I feel 
> like this is a really kludgey way to accomplish this.  Is there 
> a better way?

Couldn't you just do

arsort($chance);
$lastItem = chance[( count( $chance ) - 1 )];

?  Why iterate through the array when all you need is the last value?

thnx,
Chris

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Array difficulty

2007-07-31 Thread Chris Boget
> At this point, $result would be equal to "uppercase".  I feel 
> like this is a really kludgey way to accomplish this.  Is there 
> a better way?

Couldn't you just do

arsort($chance);
$lastItem = chance[( count( $chance ) - 1 )];

?  Why iterate through the array when all you need is the last value?

thnx,
Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array difficulty

2007-07-31 Thread Richard Davey
Hi Carlton,

Tuesday, July 31, 2007, 2:27:46 PM, you wrote:

> I have an array like this:

> $chance = array("lowercase" => 27, "uppercase" => 62, "integer" => 46);

> The values for each of the keys are randomly generated. I want to
> find the key name of the one which has the highest value. Currently, I'm 
> doing this as follows:

> arsort($chance);
> foreach ($chance as $type => $value)
> {
> $result = $type;
> break;
> }

> At this point, $result would be equal to "uppercase".  I feel like
> this is a really kludgey way to accomplish this.  Is there a better way?

Not tested it, but max() should work as the first parameter can be an
array:

http://uk3.php.net/max

Cheers,

Rich
-- 
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Array difficulty

2007-07-31 Thread Carlton Whitehead
Hi all,

I have an array like this:

$chance = array("lowercase" => 27, "uppercase" => 62, "integer" => 46); 

The values for each of the keys are randomly generated. I want to find the key 
name of the one which has the highest value. Currently, I'm doing this as 
follows:

arsort($chance);
foreach ($chance as $type => $value)
{
$result = $type;
break;
}

At this point, $result would be equal to "uppercase".  I feel like this is a 
really kludgey way to accomplish this.  Is there a better way?

Regards, 
Carlton Whitehead

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php