Prev: Sort two coupled arrays
Next: [PHP] Beginner's question: How to run a PHP web application locally?
From: Andrew Ballard on 8 Apr 2010 10:06 On Thu, Apr 8, 2010 at 9:55 AM, tedd <tedd.sperling(a)gmail.com> wrote: > At 8:28 AM -0400 4/8/10, Andrew Ballard wrote: >> >> On Wed, Apr 7, 2010 at 6:46 PM, Ryan Sun <ryansun81(a)gmail.com> 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 > Yes, but looking for alternative array sorts in the manual will lead you to arsort(). :-) <?php $a = array ( 1 => '75', 2 => '31', 3 => '31', 4 => '31', 5 => '40', ); $b = array ( 1 => 'Personal Email', 2 => 'Personal Phone', 3 => 'Web site', 4 => 'Text Message', 5 => 'USPS mail', ); $x = array_combine($b, $a); var_export($x); /* array ( 'Personal Email' => '75', 'Personal Phone' => '31', 'Web site' => '31', 'Text Message' => '31', 'USPS mail' => '40', ) */ echo "\n"; arsort($x); var_export($x); /* array ( 'Personal Email' => '75', 'USPS mail' => '40', 'Text Message' => '31', 'Web site' => '31', 'Personal Phone' => '31', ) */ ?> Andrew
From: Robert Cummings on 8 Apr 2010 10:26 tedd wrote: > At 8:28 AM -0400 4/8/10, Andrew Ballard wrote: >> On Wed, Apr 7, 2010 at 6:46 PM, Ryan Sun <ryansun81(a)gmail.com> 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? <?php 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
First
|
Prev
|
Pages: 1 2 Prev: Sort two coupled arrays Next: [PHP] Beginner's question: How to run a PHP web application locally? |