RE: [PHP] Sorting an array of arrays....
Good day, uasort() should do what you need. http://www.php.net/manual/en/function.uasort.php There's an example for the usort function that does something similar to what you want, in fact. Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -Original Message- From: Fifield, Mike [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 14, 2002 9:26 AM To: [EMAIL PROTECTED] Subject: [PHP] Sorting an array of arrays What I am trying to do is sort a array of arrays but I want to sort by one of the pieces of data stored in the arrays inside the array. For example; $data[blue] = array(name, age, time, 3); $data[green] = array(name, age, time, 7); $data[red] = array(name, age, time, 6); $data[yellow] = array(name, age, time, 2); $data[white] = array(name, age, time, 1); $data[black] = array(name, age, time, 9); I want to be able to sort this array of arrays by the very last element, the numbers. Mike Fifield Charles Schwab Co, Inc. WARNING: All e-mail sent to or from this address will be received by the Charles Schwab corporate e-mail system and is subject to archival and review by someone other than the recipient. -- 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: [PHP] Sorting an array of arrays....
Does anyone have an example of code that does this, I have been trying to decipher the instructions on the use of uasort but it does not make a hole lot of sense. They are using static keys and sorting by one of the array elements of each of there static keys. It does not seem like one should have to do it this way. I need it to go through each key and sort the keys based on a value in the array held in that key. Thanks Mike -Original Message- From: Darren Gamble [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 14, 2002 9:35 AM To: 'Fifield, Mike'; [EMAIL PROTECTED] Subject: RE: [PHP] Sorting an array of arrays Good day, uasort() should do what you need. http://www.php.net/manual/en/function.uasort.php There's an example for the usort function that does something similar to what you want, in fact. Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -Original Message- From: Fifield, Mike [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 14, 2002 9:26 AM To: [EMAIL PROTECTED] Subject: [PHP] Sorting an array of arrays What I am trying to do is sort a array of arrays but I want to sort by one of the pieces of data stored in the arrays inside the array. For example; $data[blue] = array(name, age, time, 3); $data[green] = array(name, age, time, 7); $data[red] = array(name, age, time, 6); $data[yellow] = array(name, age, time, 2); $data[white] = array(name, age, time, 1); $data[black] = array(name, age, time, 9); I want to be able to sort this array of arrays by the very last element, the numbers. Mike Fifield Charles Schwab Co, Inc. WARNING: All e-mail sent to or from this address will be received by the Charles Schwab corporate e-mail system and is subject to archival and review by someone other than the recipient. -- 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: [PHP] Sorting an array of arrays....
On Thu, 2002-02-14 at 08:26, Fifield, Mike wrote: What I am trying to do is sort a array of arrays but I want to sort by one of the pieces of data stored in the arrays inside the array. For example; $data[blue] = array(name, age, time, 3); $data[green] = array(name, age, time, 7); $data[red] = array(name, age, time, 6); $data[yellow] = array(name, age, time, 2); $data[white] = array(name, age, time, 1); $data[black] = array(name, age, time, 9); I want to be able to sort this array of arrays by the very last element, the numbers. ?php error_reporting(E_ALL); $data = array(); $data['blue'] = array(name, age, time, 3); $data['green'] = array(name, age, time, 7); $data['red'] = array(name, age, time, 6); $data['yellow'] = array(name, age, time, 2); $data['white'] = array(name, age, time, 1); $data['black'] = array(name, age, time, 9); $data['spooge'] = array(); function cmp($a, $b) { /* Bounds checking. To put any empty arrays at the * end instead of the beginning, swap -1 and 1 in the * next two lines. */ if (empty($a) !empty($b)) return -1; if (empty($b) !empty($a)) return 1; if (empty($a) empty($b)) return 0; return (int) ($a[count($a) - 1] - $b[count($b) - 1]); } uasort($data, 'cmp'); print_r($data); ? Hope this helps, Torben Mike Fifield Charles Schwab Co, Inc. WARNING: All e-mail sent to or from this address will be received by the Charles Schwab corporate e-mail system and is subject to archival and review by someone other than the recipient. -- Torben Wilson [EMAIL PROTECTED] http://www.thebuttlesschaps.com http://www.hybrid17.com http://www.inflatableeye.com +1.604.709.0506 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array of arrays....
try to add to your script: function cmp ($a, $b) { return strcmp($a[3],$b[3]); } usort($data, cmp); Fifield, Mike [EMAIL PROTECTED] wrote: What I am trying to do is sort a array of arrays but I want to sort by one of the pieces of data stored in the arrays inside the array. For example; $data[blue] = array(name, age, time, 3); $data[green] = array(name, age, time, 7); $data[red] = array(name, age, time, 6); $data[yellow] = array(name, age, time, 2); $data[white] = array(name, age, time, 1); $data[black] = array(name, age, time, 9); I want to be able to sort this array of arrays by the very last element, the numbers. Mike Fifield Charles Schwab Co, Inc. WARNING: All e-mail sent to or from this address will be received by the Charles Schwab corporate e-mail system and is subject to archival and review by someone other than the recipient. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php - Do You Yahoo!? Send FREE Valentine eCards with Yahoo! Greetings!