Re: [PHP] Sorting multidimensional arrays
Result. Cheers! On 11/16/06, Robert Cummings <[EMAIL PROTECTED]> wrote: On Thu, 2006-11-16 at 15:28 +, Dave Goodchild wrote: > Hi all. I have a multidimensional array here: > > Bums" ["name"]=> string(13) "Tits And Bums" [3]=> string(19) "The Pleasure > > [--SNIP--] > > ...which comprises a set of returned results for an events search - with the > date computed dynamically and added to the end of the array in each case. > What I want to do with this data is display the array in an html table, > sorted by the date. At the moment the display shows each event from earliest > to last date, then the next event from earliest to last etc. What I want to > do is display all the data from earliest to last date - my question is, how > do I sort the entire array based on the date value in the second level > array? usort() Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- http://www.web-buddha.co.uk
Re: [PHP] Sorting multidimensional arrays
On Thu, 2006-11-16 at 15:28 +, Dave Goodchild wrote: > Hi all. I have a multidimensional array here: > > Bums" ["name"]=> string(13) "Tits And Bums" [3]=> string(19) "The Pleasure > > [--SNIP--] > > ...which comprises a set of returned results for an events search - with the > date computed dynamically and added to the end of the array in each case. > What I want to do with this data is display the array in an html table, > sorted by the date. At the moment the display shows each event from earliest > to last date, then the next event from earliest to last etc. What I want to > do is display all the data from earliest to last date - my question is, how > do I sort the entire array based on the date value in the second level > array? usort() Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sorting multidimensional arrays
Hi all. I have a multidimensional array here: array(6) { [0]=> array(25) { [0]=> string(1) "7" ["eventid"]=> string(1) "7" [1]=> string(2) "17" ["cat_id"]=> string(2) "17" [2]=> string(13) "Tits And Bums" ["name"]=> string(13) "Tits And Bums" [3]=> string(19) "The Pleasure Centre" ["venue"]=> string(19) "The Pleasure Centre" [4]=> string(17) "�8.00 per session" ["fee"]=> string(17) "�8.00 per session" [5]=> string(12) "01297 453555" ["c_phone"]=> string(12) "01297 453555" [6]=> string(0) "" ["c_site"]=> string(0) "" [7]=> string(4) "RG29" ["postcode"]=> string(4) "RG29" [8]=> string(10) "2006-12-17" ["start_date"]=> string(10) "2006-12-17" [9]=> string(10) "2007-08-18" ["end_date"]=> string(10) "2007-08-18" [10]=> string(5) "16.00" ["start_time"]=> string(5) "16.00" [11]=> string(6) "weekly" ["frequency"]=> string(6) "weekly" ["date"]=> int(1171753200) } [1]=> array(25) { [0]=> string(1) "7" ["eventid"]=> string(1) "7" [1]=> string(2) "17" ["cat_id"]=> string(2) "17" [2]=> string(13) "Tits And Bums" ["name"]=> string(13) "Tits And Bums" [3]=> string(19) "The Pleasure Centre" ["venue"]=> string(19) "The Pleasure Centre" [4]=> string(17) "�8.00 per session" ["fee"]=> string(17) "�8.00 per session" [5]=> string(12) "01297 453555" ["c_phone"]=> string(12) "01297 453555" [6]=> string(0) "" ["c_site"]=> string(0) "" [7]=> string(4) "RG29" ["postcode"]=> string(4) "RG29" [8]=> string(10) "2006-12-17" ["start_date"]=> string(10) "2006-12-17" [9]=> string(10) "2007-08-18" ["end_date"]=> string(10) "2007-08-18" [10]=> string(5) "16.00" ["start_time"]=> string(5) "16.00" [11]=> string(6) "weekly" ["frequency"]=> string(6) "weekly" ["date"]=> int(1172358000) } [2]=> array(25) { [0]=> string(2) "10" ["eventid"]=> string(2) "10" [1]=> string(2) "17" ["cat_id"]=> string(2) "17" [2]=> string(16) "Fitness For Life" ["name"]=> string(16) "Fitness For Life" [3]=> string(19) "The Pleasure Centre" ["venue"]=> string(19) "The Pleasure Centre" [4]=> string(17) "�8.00 per session" ["fee"]=> string(17) "�8.00 per session" [5]=> string(12) "01297 453555" ["c_phone"]=> string(12) "01297 453555" [6]=> string(0) "" ["c_site"]=> string(0) "" [7]=> string(4) "RG29" ["postcode"]=> string(4) "RG29" [8]=> string(10) "2006-12-17" ["start_date"]=> string(10) "2006-12-17" [9]=> string(10) "2007-12-23" ["end_date"]=> string(10) "2007-12-23" [10]=> string(5) "16.00" ["start_time"]=> string(5) "16.00" [11]=> string(6) "weekly" ["frequency"]=> string(6) "weekly" ["date"]=> int(1171753200) } [3]=> array(25) { [0]=> string(2) "10" ["eventid"]=> string(2) "10" [1]=> string(2) "17" ["cat_id"]=> string(2) "17" [2]=> string(16) "Fitness For Life" ["name"]=> string(16) "Fitness For Life" [3]=> string(19) "The Pleasure Centre" ["venue"]=> string(19) "The Pleasure Centre" [4]=> string(17) "�8.00 per session" ["fee"]=> string(17) "�8.00 per session" [5]=> string(12) "01297 453555" ["c_phone"]=> string(12) "01297 453555" [6]=> string(0) "" ["c_site"]=> string(0) "" [7]=> string(4) "RG29" ["postcode"]=> string(4) "RG29" [8]=> string(10) "2006-12-17" ["start_date"]=> string(10) "2006-12-17" [9]=> string(10) "2007-12-23" ["end_date"]=> string(10) "2007-12-23" [10]=> string(5) "16.00" ["start_time"]=> string(5) "16.00" [11]=> string(6) "weekly" ["frequency"]=> string(6) "weekly" ["date"]=> int(1172358000) } [4]=> array(25) { [0]=> string(2) "11" ["eventid"]=> string(2) "11" [1]=> string(2) "30" ["cat_id"]=> string(2) "30" [2]=> string(15) "The Flea Market" ["name"]=> string(15) "The Flea Market" [3]=> string(18) "The Covered Market" ["venue"]=> string(18) "The Covered Market" [4]=> string(1) "0" ["fee"]=> string(1) "0" [5]=> string(12) "0186 577" ["c_phone"]=> string(12) "0186 577" [6]=> string(0) "" ["c_site"]=> string(0) "" [7]=> string(4) "SW20" ["postcode"]=> string(4) "SW20" [8]=> string(10) "2006-12-17" ["start_date"]=> string(10) "2006-12-17" [9]=> string(10) "2007-12-17" ["end_date"]=> string(10) "2007-12-17" [10]=> string(5) "16.00" ["start_time"]=> string(5) "16.00" [11]=> string(8) "biweekly" ["frequency"]=> string(8) "biweekly" ["date"]=> int(1172358000) } [5]=> array(25) { [0]=> string(1) "4" ["eventid"]=> string(1) "4" [1]=> string(2) "21" ["cat_id"]=> string(2) "21" [2]=> string(16) "The Kickabout II" ["name"]=> string(16) "The Kickabout II" [3]=> string(18) "The Football Pitch" ["venue"]=> string(18) "The Football Pitch" [4]=> string(1) "0" ["fee"]=> string(1) "0" [5]=> string(12) "01737 453666" ["c_phone"]=> string(12) "01737 453666" [6]=> string(0) "" ["c_site"]=> string(0) "" [7]=> string(3) "RH1" ["postcode"]=> string(3) "RH1" [8]=> string(10) "2007-02-19" ["start_date"]=> string(10) "2007-02-19" [9]=> string(10) "-00-00" ["end_date"]=> string(10) "-00-00" [10]=> string(4) "3.00" ["start_time"]=> string(4) "3.00" [11]=> string(6) "single" ["frequency"]=> string(6) "single" ["date"]=> int(1171839600) } } ...which comprises a set of returned results for an events search - with the date computed dynamically and added to the e
Re: [PHP] Sorting multidimensional arrays
On Thu, 13 Feb 2003, Sean Brown wrote: > Let's say I've got an array like so: > > $myarray[0]["firstname"] = "Mickey"; > $myarray[0]["lastname"] = "Mouse"; > $myarray[0]["score"] = 20; > > $myarray[1]["firstname"] = "Donald"; > $myarray[1]["lastname"] = "Duck"; > $myarray[1]["score"] = 10; > > I'd like be able to sort the array using different dimensions before I > cycle through a while loop to print out the results. For instance, by > lastname, or by score. I can't seem to get array_multisort() to do it. array_multisort() isn't what you want. > Any ideas? I've seen this question too many times go through here w/ lots of links, and no code ... so here ya go. (And I think someone else asked a similar question today too ... you too, listen up ;) You can use the usort() function to your advantage. This is how: Obviously, you could sort by the other keys by changing the value of $sortKey, in the code above, or sort in reverse order by flipping the inequality comparison operator in myCompare(). http://www.php.net/manual/en/function.usort.php ... for all your usort() goodness. g.luck, ~Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sorting multidimensional arrays
Let's say I've got an array like so: $myarray[0]["firstname"] = "Mickey"; $myarray[0]["lastname"] = "Mouse"; $myarray[0]["score"] = 20; $myarray[1]["firstname"] = "Donald"; $myarray[1]["lastname"] = "Duck"; $myarray[1]["score"] = 10; I'd like be able to sort the array using different dimensions before I cycle through a while loop to print out the results. For instance, by lastname, or by score. I can't seem to get array_multisort() to do it. Any ideas? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sorting multidimensional arrays..
> -Original Message- > From: Chad Day [mailto:[EMAIL PROTECTED]] > Sent: 30 January 2003 19:29 > > I'm struggling with array_multisort, was hoping someone could > give me some > help. > > I have a multidim array, such as: > > $myarray[0][FIRSTNAME] = JOE > $myarray[1][FIRSTNAME] = TIM > $myarray[2][FIRSTNAME] = BOB > > $myarray[0][LASTNAME] = SMITH > $myarray[1][LASTNAME] = BROWN > $myarray[2][LASTNAME] = JONES > > $myarray[0][EXTENSION] = 2000 > $myarray[1][EXTENSION] = 4000 > $myarray[2][EXTENSION] = 1000 > > I was trying array_multisort($myarray[EXTENSION], > SORT_NUMERIC, SORT_DESC), You won't be able to use array_multisort() to do this, as your indexes are the wrong way round. It looks to me like the only solution is using usort() on $myarray, with a callback function that sorts on the [EXTENSION] element; so, something like: function compare($a, $b) { return $a[EXTENSION]==$b[EXTENSION]?0:($a[EXTENSION]<$b[EXTENSION]?-1:1) } usort($myarray, "compare"); Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting multidimensional arrays..
if 'EXTENSION' is unique then you can make life a lot easier by making that the key of the root array (this is a heirarchy of arrays). Tim Ward http://www.chessish.com mailto:[EMAIL PROTECTED] - Original Message - From: Chad Day <[EMAIL PROTECTED]> To: php general <[EMAIL PROTECTED]> Sent: Thursday, January 30, 2003 7:29 PM Subject: [PHP] Sorting multidimensional arrays.. > I'm struggling with array_multisort, was hoping someone could give me some > help. > > I have a multidim array, such as: > > $myarray[0][FIRSTNAME] = JOE > $myarray[1][FIRSTNAME] = TIM > $myarray[2][FIRSTNAME] = BOB > > $myarray[0][LASTNAME] = SMITH > $myarray[1][LASTNAME] = BROWN > $myarray[2][LASTNAME] = JONES > > $myarray[0][EXTENSION] = 2000 > $myarray[1][EXTENSION] = 4000 > $myarray[2][EXTENSION] = 1000 > > I was trying array_multisort($myarray[EXTENSION], SORT_NUMERIC, SORT_DESC), > but nothing seems to be happening to the array. If anyone has any clues or > can point me in the right direction, I would appreciate it. > > Thanks, > Chad > > > -- > 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
[PHP] Sorting multidimensional arrays..
I'm struggling with array_multisort, was hoping someone could give me some help. I have a multidim array, such as: $myarray[0][FIRSTNAME] = JOE $myarray[1][FIRSTNAME] = TIM $myarray[2][FIRSTNAME] = BOB $myarray[0][LASTNAME] = SMITH $myarray[1][LASTNAME] = BROWN $myarray[2][LASTNAME] = JONES $myarray[0][EXTENSION] = 2000 $myarray[1][EXTENSION] = 4000 $myarray[2][EXTENSION] = 1000 I was trying array_multisort($myarray[EXTENSION], SORT_NUMERIC, SORT_DESC), but nothing seems to be happening to the array. If anyone has any clues or can point me in the right direction, I would appreciate it. Thanks, Chad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sorting Multidimensional Arrays
check http://www.php.net/manual/en/function.asort.php there's a routine someone wrote in the contributed section... -jack -Original Message- From: Rick Dietz [mailto:[EMAIL PROTECTED]] Sent: Monday, May 07, 2001 1:21 PM To: [EMAIL PROTECTED] Subject: [PHP] Sorting Multidimensional Arrays I'm having trouble finding a function to sort a multidimensional array by the value of a given key in the contained arrays. Perhaps i'm missing something, perhaps PHP is missing something. Take the array below. All I'm looking to do is reorder $my_array based on the value of the "name" key for example--to order $my_array based on the alphabetical order of the "names". $my_array = array ( array ("name"=>"Sam", "age"=>"20", married="yes"), array ("name"=>"Alice", "age"=>"25", married="no"), array ("name"=>"Jim", "age"=>"19", married="no"), array ("name"=>"Janice", "age"=>"22", married="yes") ); Am I missing something that already exists? -Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Sorting Multidimensional Arrays
I'm having trouble finding a function to sort a multidimensional array by the value of a given key in the contained arrays. Perhaps i'm missing something, perhaps PHP is missing something. Take the array below. All I'm looking to do is reorder $my_array based on the value of the "name" key for example--to order $my_array based on the alphabetical order of the "names". $my_array = array ( array ("name"=>"Sam", "age"=>"20", married="yes"), array ("name"=>"Alice", "age"=>"25", married="no"), array ("name"=>"Jim", "age"=>"19", married="no"), array ("name"=>"Janice", "age"=>"22", married="yes") ); Am I missing something that already exists? -Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]