Re: Sort two coupled arrays {my solution]

Re: Sort two coupled arrays {my solution]

am 08.04.2010 16:06:08 von Andrew Ballard

On Thu, Apr 8, 2010 at 9:55 AM, 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' =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
>
> 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://earthston=
es.com
>

Yes, but looking for alternative array sorts in the manual will lead
you to arsort(). :-)


$a =3D array
(
1 =3D> '75',
2 =3D> '31',
3 =3D> '31',
4 =3D> '31',
5 =3D> '40',
);

$b =3D array
(
1 =3D> 'Personal Email',
2 =3D> 'Personal Phone',
3 =3D> 'Web site',
4 =3D> 'Text Message',
5 =3D> 'USPS mail',
);

$x =3D array_combine($b, $a);
var_export($x);
/*
array (
'Personal Email' =3D> '75',
'Personal Phone' =3D> '31',
'Web site' =3D> '31',
'Text Message' =3D> '31',
'USPS mail' =3D> '40',
)
*/

echo "\n";

arsort($x);

var_export($x);
/*
array (
'Personal Email' =3D> '75',
'USPS mail' =3D> '40',
'Text Message' =3D> '31',
'Web site' =3D> '31',
'Personal Phone' =3D> '31',
)
*/

?>

Andrew

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