From: tedd on 7 Apr 2010 16:09 Hi gang: Here's the problem -- I want to sort and combine two arrays into one sorted array. Here's a real-world example: Array 1 ( [1] => 75 [2] => 31 [3] => 31 [4] => 31 [5] => 40 ) Array 2 ( [1] => Personal Email [2] => Personal Phone [3] => Web site [4] => Text Message [5] => USPS mail ) After the operation, I want this: Array ( [75] => Personal Email [40] => USPS mail [31] => Personal Phone [31] => Web site [31] => Text Message ) Note: This is a descending-sort of Array 1 while being coupled to index of Array 2. In other words, the order of Array 2 depends upon the order of Array 1 -- the two arrays are coupled. I've solved this problem, but my solution is pretty lame. There has to be a better/slicker way. Suggestions? Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com From: Piero Steinger on 7 Apr 2010 16:26 Am 07.04.2010 22:09, schrieb tedd:> Hi gang: > > Here's the problem -- I want to sort and combine two arrays into one > sorted array. Here's a real-world example: > > Array 1 > ( > [1] => 75 > [2] => 31 > [3] => 31 > [4] => 31 > [5] => 40 > ) > > Array 2 > ( > [1] => Personal Email > [2] => Personal Phone > [3] => Web site > [4] => Text Message > [5] => USPS mail > ) > > After the operation, I want this: > > Array > ( > [75] => Personal Email > [40] => USPS mail > [31] => Personal Phone > [31] => Web site > [31] => Text Message > ) > > Note: This is a descending-sort of Array 1 while being coupled to > index of Array 2. In other words, the order of Array 2 depends upon > the order of Array 1 -- the two arrays are coupled. > > I've solved this problem, but my solution is pretty lame. There has to > be a better/slicker way. > > Suggestions? > > Cheers, > > tedd array_combine(\$key_array, \$value_array) :) -- Piero From: Mattias Thorslund on 7 Apr 2010 16:36 Piero Steinger wrote:> Am 07.04.2010 22:09, schrieb tedd: > >> Hi gang: >> >> Here's the problem -- I want to sort and combine two arrays into one >> sorted array. Here's a real-world example: >> >> Array 1 >> ( >> [1] => 75 >> [2] => 31 >> [3] => 31 >> [4] => 31 >> [5] => 40 >> ) >> >> Array 2 >> ( >> [1] => Personal Email >> [2] => Personal Phone >> [3] => Web site >> [4] => Text Message >> [5] => USPS mail >> ) >> >> After the operation, I want this: >> >> Array >> ( >> [75] => Personal Email >> [40] => USPS mail >> [31] => Personal Phone >> [31] => Web site >> [31] => Text Message >> ) >> >> Note: This is a descending-sort of Array 1 while being coupled to >> index of Array 2. In other words, the order of Array 2 depends upon >> the order of Array 1 -- the two arrays are coupled. >> >> I've solved this problem, but my solution is pretty lame. There has to >> be a better/slicker way. >> >> Suggestions? >> >> Cheers, >> >> tedd >> > > > array_combine(\$key_array, \$value_array) > > :) > > -- Piero > And then: krsort(\$combined_array); Cheers, Mattias From: Paul M Foster on 7 Apr 2010 17:02 On Wed, Apr 07, 2010 at 04:09:47PM -0400, tedd wrote: > Hi gang: > > Here's the problem -- I want to sort and combine two arrays into one > sorted array. Here's a real-world example: > > Array 1 > ( > [1] => 75 > [2] => 31 > [3] => 31 > [4] => 31 > [5] => 40 > ) > > Array 2 > ( > [1] => Personal Email > [2] => Personal Phone > [3] => Web site > [4] => Text Message > [5] => USPS mail > ) > > After the operation, I want this: > > Array > ( > [75] => Personal Email > [40] => USPS mail > [31] => Personal Phone > [31] => Web site > [31] => Text Message > ) > > Note: This is a descending-sort of Array 1 while being coupled to > index of Array 2. In other words, the order of Array 2 depends upon > the order of Array 1 -- the two arrays are coupled. > > I've solved this problem, but my solution is pretty lame. There has > to be a better/slicker way. Just so I understand the way arrays work in PHP (gee, I *thought* I did!), as you add the final three elements in the final array, won't they overwrite each other? I was under the impression that a *numerically* indexed array has a constraint that the numeric indexes be unique, if not contiguous. Am I wrong? If so, please provide a reference. Or are those "numbers" really strings? Paul -- Paul M. Foster From: Andrew Ballard on 7 Apr 2010 17:35 On Wed, Apr 7, 2010 at 5:02 PM, Paul M Foster wrote:> On Wed, Apr 07, 2010 at 04:09:47PM -0400, tedd wrote: > >> Hi gang: >> >> Here's the problem -- I want to sort and combine two arrays into one >> sorted array. Here's a real-world example: >> >> Array 1 >> ( >> Â  Â  [1] => 75 >> Â  Â  [2] => 31 >> Â  Â  [3] => 31 >> Â  Â  [4] => 31 >> Â  Â  [5] => 40 >> ) >> >> Array 2 >> ( >> Â  Â  [1] => Personal Email >> Â  Â  [2] => Personal Phone >> Â  Â  [3] => Web site >> Â  Â  [4] => Text Message >> Â  Â  [5] => USPS mail >> ) >> >> After the operation, I want this: >> >> Array >> ( >> Â  Â  [75] => Personal Email >> Â  Â  [40] => USPS mail >> Â  Â  [31] => Personal Phone >> Â  Â  [31] => Web site >> Â  Â  [31] => Text Message >> ) [snip]> Just so I understand the way arrays work in PHP (gee, I *thought* I > did!), as you add the final three elements in the final array, won't > they overwrite each other? I was under the impression that a > *numerically* indexed array has a constraint that the numeric indexes be > unique, if not contiguous. Am I wrong? If so, please provide a > reference. Or are those "numbers" really strings? > > Paul > > -- > Paul M. Foster Array indexes have to be unique regardless of whether they are numeric or strings. '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(\$a, \$b); var_export(\$x); /* array ( 75 => 'Personal Email', 31 => 'Text Message', 40 => 'USPS mail', ) */ echo "\n"; krsort(\$x); var_export(\$x); /* array ( 75 => 'Personal Email', 40 => 'USPS mail', 31 => 'Text Message', ) */ ?> Andrew  |  Next  |  Last Pages: 1 2 Prev: Greate day for you,Next: Sort two coupled arrays {my solution]