Re: Sort two coupled arrays {my solution]

Re: Sort two coupled arrays {my solution]

am 08.04.2010 00:46:20 von Ryan Sun

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


On Wed, Apr 7, 2010 at 6:29 PM, tedd wrote:
> At 5:35 PM -0400 4/7/10, Andrew Ballard wrote:
>>
>> On Wed, Apr 7, 2010 at 5:02 PM, Paul M Foster
>> wrote:
>> Array indexes have to be unique regardless of whether they are numeric
>> or strings.
>
> Ahhh, so you start to see the problem, eh?
>
> Let's look at the problem again (a vote collection problem):
>
> Array 1
> (
> =A0 =A0[1] =3D> 75
> =A0 =A0[2] =3D> 31
> =A0 =A0[3] =3D> 31
> =A0 =A0[4] =3D> 31
> =A0 =A0[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
> (
> =A0 =A0[1] =3D> Personal Email
> =A0 =A0[2] =3D> Personal Phone
> =A0 =A0[3] =3D> Web site
> =A0 =A0[4] =3D> Text Message
> =A0 =A0[5] =3D> USPS mail
> )
>
> Array 2 is an array that contains the names for the items ($items[] ) vot=
ed
> upon. As such, index 1 (Personal Email) received 75 votes.
>
> Now, I have this data in two different arrays and I wanted to combine the
> data into one array and then preform a descend sort.
>
> This is the way I solved it:
>
> $final =3D array();
>
> for($i =3D1; $i <=3D5; $i++)
> =A0 {
> =A0 $final[$i][] =3D $votes[$i];
> =A0 $final[$i][] =3D $items[$i];
> =A0 }
>
> echo("

");
> echo('
');
> print_r($final);
> echo('
');
>
> array_multisort($final, SORT_DESC);
>
> echo('
');
> print_r($final);
> echo('
');
> echo("
");
>
> I was hoping that someone might present something clever.
>
> Cheers,
>
> tedd
>
>
> --
> -------
> http://sperling.com =A0http://ancientstones.com =A0http://earthstones.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.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 08.04.2010 15:53:32 von TedD

At 6:46 PM -0400 4/7/10, 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
>

Logically, combine_array() will work to combine the two arrays as you
said, as shown here:

http://www.webbytedd.com/bbbb/array-combine/

I've tried rsort(), but is does not solve the problem presented,
which was to sort the items depending upon their values.

Thus far, my solution is the only one that works.

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