Re: Sort two coupled arrays {my solution]

Re: Sort two coupled arrays {my solution]

am 08.04.2010 14:28:17 von Andrew Ballard

On Wed, Apr 7, 2010 at 6:46 PM, Ryan Sun wrote:
> On Wed, Apr 7, 2010 at 6:29 PM, tedd wrote:

[snip]

>>
>> Let's look at the problem again (a vote collection problem):
>>
>> Array 1
>> (
>>    [1] =3D> 75
>>    [2] =3D> 31
>>    [3] =3D> 31
>>    [4] =3D> 31
>>    [5] =3D> 40
>> )
>>
>> Array 1 is an array that contains the count of votes ($votes[] ) for the
>> index. IOW, index 1 received 75 votes.
>>
>> Array 2
>> (
>>    [1] =3D> Personal Email
>>    [2] =3D> Personal Phone
>>    [3] =3D> Web site
>>    [4] =3D> Text Message
>>    [5] =3D> USPS mail
>> )
>>
>> Array 2 is an array that contains the names for the items ($items[] ) vo=
ted
>> upon. As such, index 1 (Personal Email) received 75 votes.
>>
[snip]
>
> rsort(array_combine(array2, array1));
>
> you should expect array(
> 'Personal Email' =3D> 75,
> 'USPS mail' =3D> 40,
> 'Personal Phone' =3D> 31,
> 'Web site' =3D> 31,
> 'Text Message' =3D> 31
> )
>
> logically, the items are your key but not the count of votes
>

That's the ticket. The solution is pretty simple now that we
understand the nature of the problem. :-)

Andrew

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

Re: Sort two coupled arrays {my solution]

am 08.04.2010 15:55:53 von TedD

At 8:28 AM -0400 4/8/10, Andrew Ballard wrote:
>On Wed, Apr 7, 2010 at 6:46 PM, Ryan Sun wrote:
>
> >
>> rsort(array_combine(array2, array1));
>>
>> you should expect array(
>> 'Personal Email' => 75,
>> 'USPS mail' => 40,
>> 'Personal Phone' => 31,
>> 'Web site' => 31,
>> 'Text Message' => 31
>> )
>>
>> logically, the items are your key but not the count of votes
>>
>
>That's the ticket. The solution is pretty simple now that we
>understand the nature of the problem. :-)
>
>Andrew

Andrew:

Half the solution is understanding the problem.

However, the above solution only solves half the problem.

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

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

Re: Sort two coupled arrays {my solution]

am 08.04.2010 16:26:05 von Robert Cummings

tedd wrote:
> At 8:28 AM -0400 4/8/10, Andrew Ballard wrote:
>> On Wed, Apr 7, 2010 at 6:46 PM, Ryan Sun wrote:
>>
>> >
>>> rsort(array_combine(array2, array1));
>>>
>>> you should expect array(
>>> 'Personal Email' => 75,
>>> 'USPS mail' => 40,
>>> 'Personal Phone' => 31,
>>> 'Web site' => 31,
>>> 'Text Message' => 31
>>> )
>>>
>>> logically, the items are your key but not the count of votes
>>>
>> That's the ticket. The solution is pretty simple now that we
>> understand the nature of the problem. :-)
>>
>> Andrew
>
> Andrew:
>
> Half the solution is understanding the problem.
>
> However, the above solution only solves half the problem.

Maybe I'm confused... but can't the following be used?


array_multisort( $votes, SORT_DESC, $items, SORT_DESC );
$final = array_combine( $items, $votes );

print_r( $final );

?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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

Re: Sort two coupled arrays [my solution]

am 09.04.2010 15:44:41 von TedD

At 10:26 AM -0400 4/8/10, Robert Cummings wrote:
>tedd wrote:
>>At 8:28 AM -0400 4/8/10, Andrew Ballard wrote:
>>>On Wed, Apr 7, 2010 at 6:46 PM, Ryan Sun wrote:
>>>
>>> >
>>>> rsort(array_combine(array2, array1));
>>>>
>>>> you should expect array(
>>>> 'Personal Email' => 75,
>>>> 'USPS mail' => 40,
>>>> 'Personal Phone' => 31,
>>>> 'Web site' => 31,
>>>> 'Text Message' => 31
>>>> )
>>>>
>>>> logically, the items are your key but not the count of votes
>>>>
>>>That's the ticket. The solution is pretty simple now that we
>>>understand the nature of the problem. :-)
>>>
>>>Andrew
>>
>>Andrew:
>>
>>Half the solution is understanding the problem.
>>
>>However, the above solution only solves half the problem.
>
>Maybe I'm confused... but can't the following be used?
>
> >
> array_multisort( $votes, SORT_DESC, $items, SORT_DESC );
> $final = array_combine( $items, $votes );
>
> print_r( $final );
>
>?>
>
>Cheers,
>Rob.

Rob:

You're never confused because you are always right.

Congrats, you were the first to solve this problem this simply.

To tell the truth, I didn't fully understand how array_multisort()
worked until I reviewed your solution. I thought array_multisort()
was to sort multi-dimensional arrays and simply overlooked the
description that it could also sort several arrays at once. Duh --
sometimes my reading/comprehension skills suck.

Many thanks for your time.

Cheers,

tedd


--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

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

Re: Sort two coupled arrays [my solution]

am 09.04.2010 16:09:05 von Robert Cummings

tedd wrote:
> Rob:
>
> You're never confused because you are always right.

I should have you mention this to my wife... I'll provide the helmet >:)

> Congrats, you were the first to solve this problem this simply.
>
> To tell the truth, I didn't fully understand how array_multisort()
> worked until I reviewed your solution. I thought array_multisort()
> was to sort multi-dimensional arrays and simply overlooked the
> description that it could also sort several arrays at once. Duh --
> sometimes my reading/comprehension skills suck.
>
> Many thanks for your time.

No prob... was the first time I read up on multisort too :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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