[PHP] sorting multi-dimensional arrays
Hi All, I've read the manual several times on sorting multi-dimensional arrays, but I can't seem to wrap my mind around what I need to do to accomplish my goal. //this is what I'm doing: (stepping through a result set, and putting it into a multi-dimensional array. while ($row = mysql_fetch_array($result)) { $docs[$counter][doc_id] = $row[doc_id]; $docs[$counter][doc_name] = $row[doc_name]; $docs[$counter][clinic_id] = $row[clinic_id]; $docs[$counter][clinic_name] = $row[clinic_name]; $counter++; } //end while //now, I want that array sorted by the clinic_name, and then doc_name. Can someone who has more currently active brain cells than me figure out what the array_multisort function call should look like? Thanks in advance for any help proffered! It is greatly appreciated! Katie Dewees Web Developer E-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi-dimensional arrays
I've read the manual several times on sorting multi-dimensional arrays, but I can't seem to wrap my mind around what I need to do to accomplish my goal. //this is what I'm doing: (stepping through a result set, and putting it into a multi-dimensional array. while ($row = mysql_fetch_array($result)) { $docs[$counter][doc_id] = $row[doc_id]; $docs[$counter][doc_name] = $row[doc_name]; $docs[$counter][clinic_id] = $row[clinic_id]; $docs[$counter][clinic_name] = $row[clinic_name]; $counter++; } //end while //now, I want that array sorted by the clinic_name, and then doc_name. Not to be a stinker, but why not just do the sorting in your mysql query? select * from your_table order by clinic_name, doc_name; If sorting in PHP is what you really need to do, the answer is to not use array_multisort. Use usort() instead, with a function that compares arguments based on clinic_name and doc_name (code untested): function compare_docs($a, $b) { if($a['clinic_name'] === $b['clinic_name']) return(strcmp($a['doc_name'], $b['doc_name']); return(strcmp($a['clinic_name'], $b['clinic_name']); } while($row = mysql_fetch_array($result)) $docs[$counter++] = $row; usort($docs, 'compare_docs'); - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sorting multi-dimensional arrays
Hi All, I am trying to sort a multi-dimensional array using a value in the second dimension. I have looked at the usort examples but don't fully understand what usort is actually doing so am struggling. I have tried the following: if (! getmxrr($this-_Domain, $as_hosts, $ai_weights)) { return false; } for ($i = 0; $i count($as_hosts); $i++) { $this-MXRecs[] = array($as_hosts[$i], $ai_weights[$i]); } function IsBestMX($a, $b) { if ($a[1] == $b[1]) { return 0; } return ($a[1] $b[1]) ? -1 : 1; } usort($this-MXRecs); Can anyone point me in the right direction? TIA Phil. --- Phil Ewington - Technical Director 43 Plc - Ashdale House 35 Broad Street, Wokingham Berkshire RG40 1AU T: +44 (0)1189 789 500 F: +44 (0)1189 784 994 E: mailto:[EMAIL PROTECTED] W: www.soyouthink.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting multi-dimensional arrays
Ooops, forgot to specify the compare function with usort(); all working now. Phil. -Original Message- From: Phil Ewington - 43 Plc [mailto:[EMAIL PROTECTED] Sent: 22 December 2003 14:08 To: [EMAIL PROTECTED] Subject: [PHP] sorting multi-dimensional arrays Hi All, I am trying to sort a multi-dimensional array using a value in the second dimension. I have looked at the usort examples but don't fully understand what usort is actually doing so am struggling. I have tried the following: if (! getmxrr($this-_Domain, $as_hosts, $ai_weights)) { return false; } for ($i = 0; $i count($as_hosts); $i++) { $this-MXRecs[] = array($as_hosts[$i], $ai_weights[$i]); } function IsBestMX($a, $b) { if ($a[1] == $b[1]) { return 0; } return ($a[1] $b[1]) ? -1 : 1; } usort($this-MXRecs); Can anyone point me in the right direction? TIA Phil. --- Phil Ewington - Technical Director 43 Plc - Ashdale House 35 Broad Street, Wokingham Berkshire RG40 1AU T: +44 (0)1189 789 500 F: +44 (0)1189 784 994 E: mailto:[EMAIL PROTECTED] W: www.soyouthink.com -- 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 Multi-Dimensional Arrays using uksort()
Hey folks: I have a function that returns an array. I'm assigning the returned array to a variable inside another function: -- $dirs = getFileArray($parentDir,title_2.txt); -- If I use print_r() on the variable it writes out as expected: -- Array ( [0] = Array ( [folderName] = 97StudioPhotos [linkText] = Studio 3/27 - 3/30 (by Matt Myrdal/VE) [sortDate] = 03/31/02 ) [1] = Array ( [folderName] = 98PromoPhotos [linkText] = Promo Photos BW (by Matt Myrdal - 3/24/02) [sortDate] = 03/24/02 ) [2] = Array ( [folderName] = 99PhotoSet1 [linkText] = Photo Set I (by Matt Myrdal - 3/12/02) [sortDate] = 03/12/02 ) ) -- Now I want to sort this array using a custom defined sort method: -- uksort($dirs,sortByDateDesc); function sortByDateDesc($a,$b) { print hra = br; print_r($a); print hr; print hrb = br; print_r($b); print hr; print $a. vs .$b.br; if ($a[sortDate] $b[sortDate]){ print .$a[sortDate]. .$b[sortDate].br; return -1; } else if ($a[sortDate] $b[sortDate]){ print .$a[sortDate]. .$b[sortDate].br; return 1; } else { print .$a[sortDate]. = .$b[sortDate].br; return 0; } } -- As you can see, I've added a bunch of print() calls for testing. The trouble is that none of the variables are being output into the print functions and all of the print_r() calls just return 1. In the end, the array is not being sorted at all. Why can't I access any of the array elements in this function and why won't it sort correctly? I've tried accessing the elements everyway I can think of: $a[sortDate] $a[sortDate] $a[2] Regards, Chris Bloom PS: Attached is an example file arr_sort_test.php Web Infrastructure Analyst Lifespan Corporate Services 167 Point Street #245 Providence, RI 02903 401-444-2074 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sorting Multi-Dimensional Arrays using uksort()
try using usort() instead of uksort() - (i haven't checked the manual but) I believe uksort is for sorting based on the keys and not the values, which is what you're trying to do -Original Message- From: Bloom, Chris [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 02, 2002 9:57 AM To: '[EMAIL PROTECTED]' Cc: Bloom, Chris Subject: [PHP] Sorting Multi-Dimensional Arrays using uksort() Hey folks: I have a function that returns an array. I'm assigning the returned array to a variable inside another function: -- $dirs = getFileArray($parentDir,title_2.txt); -- If I use print_r() on the variable it writes out as expected: -- Array ( [0] = Array ( [folderName] = 97StudioPhotos [linkText] = Studio 3/27 - 3/30 (by Matt Myrdal/VE) [sortDate] = 03/31/02 ) [1] = Array ( [folderName] = 98PromoPhotos [linkText] = Promo Photos BW (by Matt Myrdal - 3/24/02) [sortDate] = 03/24/02 ) [2] = Array ( [folderName] = 99PhotoSet1 [linkText] = Photo Set I (by Matt Myrdal - 3/12/02) [sortDate] = 03/12/02 ) ) -- Now I want to sort this array using a custom defined sort method: -- uksort($dirs,sortByDateDesc); function sortByDateDesc($a,$b) { print hra = br; print_r($a); print hr; print hrb = br; print_r($b); print hr; print $a. vs .$b.br; if ($a[sortDate] $b[sortDate]){ print .$a[sortDate]. .$b[sortDate].br; return -1; } else if ($a[sortDate] $b[sortDate]){ print .$a[sortDate]. .$b[sortDate].br; return 1; } else { print .$a[sortDate]. = .$b[sortDate].br; return 0; } } -- As you can see, I've added a bunch of print() calls for testing. The trouble is that none of the variables are being output into the print functions and all of the print_r() calls just return 1. In the end, the array is not being sorted at all. Why can't I access any of the array elements in this function and why won't it sort correctly? I've tried accessing the elements everyway I can think of: $a[sortDate] $a[sortDate] $a[2] Regards, Chris Bloom PS: Attached is an example file arr_sort_test.php Web Infrastructure Analyst Lifespan Corporate Services 167 Point Street #245 Providence, RI 02903 401-444-2074 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sorting Multi-Dimensional Arrays using uksort()
Sweet! Thanks for the help! -Original Message- From: Martin Towell [mailto:[EMAIL PROTECTED]] Sent: Monday, April 01, 2002 7:15 PM To: 'Bloom, Chris'; '[EMAIL PROTECTED]' Subject: RE: [PHP] Sorting Multi-Dimensional Arrays using uksort() try using usort() instead of uksort() - (i haven't checked the manual but) I believe uksort is for sorting based on the keys and not the values, which is what you're trying to do -Original Message- From: Bloom, Chris [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 02, 2002 9:57 AM To: '[EMAIL PROTECTED]' Cc: Bloom, Chris Subject: [PHP] Sorting Multi-Dimensional Arrays using uksort() Hey folks: I have a function that returns an array. I'm assigning the returned array to a variable inside another function: -- $dirs = getFileArray($parentDir,title_2.txt); -- If I use print_r() on the variable it writes out as expected: -- Array ( [0] = Array ( [folderName] = 97StudioPhotos [linkText] = Studio 3/27 - 3/30 (by Matt Myrdal/VE) [sortDate] = 03/31/02 ) [1] = Array ( [folderName] = 98PromoPhotos [linkText] = Promo Photos BW (by Matt Myrdal - 3/24/02) [sortDate] = 03/24/02 ) [2] = Array ( [folderName] = 99PhotoSet1 [linkText] = Photo Set I (by Matt Myrdal - 3/12/02) [sortDate] = 03/12/02 ) ) -- Now I want to sort this array using a custom defined sort method: -- uksort($dirs,sortByDateDesc); function sortByDateDesc($a,$b) { print hra = br; print_r($a); print hr; print hrb = br; print_r($b); print hr; print $a. vs .$b.br; if ($a[sortDate] $b[sortDate]){ print .$a[sortDate]. .$b[sortDate].br; return -1; } else if ($a[sortDate] $b[sortDate]){ print .$a[sortDate]. .$b[sortDate].br; return 1; } else { print .$a[sortDate]. = .$b[sortDate].br; return 0; } } -- As you can see, I've added a bunch of print() calls for testing. The trouble is that none of the variables are being output into the print functions and all of the print_r() calls just return 1. In the end, the array is not being sorted at all. Why can't I access any of the array elements in this function and why won't it sort correctly? I've tried accessing the elements everyway I can think of: $a[sortDate] $a[sortDate] $a[2] Regards, Chris Bloom PS: Attached is an example file arr_sort_test.php Web Infrastructure Analyst Lifespan Corporate Services 167 Point Street #245 Providence, RI 02903 401-444-2074 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi-dimensional arrays
it was just a typo... I meant: $categories[0][id]=2; $categories[0][name]=lele; $categories[1][id]=6; $categories[1][name]=lala; .. ... and so on... At 00:52 16/9/2001 -0400, you wrote: The below code would result in an array of: $categories[0][id]=2; $categories[0][name]=Lele; Please tell us the actual code you're using, or print_r($arrayname); /* Chris Lambert, CTO - [EMAIL PROTECTED] WhiteCrown Networks - More Than White Hats Web Application Security - www.whitecrown.net */ - Original Message - From: Christian Dechery [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Sunday, September 09, 2001 11:51 PM Subject: [PHP] sorting multi-dimensional arrays | I'm having a little trouble sorting mult-dimensional arrays by key... | | I looked-up the manual and found out that array_multisort() thing... but I | don't think it does what I want... or I don't understand it... | | I have an array like this... | | $categories=array(); | $categories[0][id]=1; | $categories[0][name]=Xyz; | $categories[0][id]=7; | $categories[0][name]=Lala; | $categories[0][id]=2; | $categories[0][name]=Lele; | | I want it sorted by name... the way I'm doing it right now is by using | an auxiliar array that gets all the names, sorts them, and then build a new | array based on that... | | is a better way? | | p.s: meu novo email é [EMAIL PROTECTED] | | . Christian Dechery (lemming) | . http://www.tanamesa.com.br | . Gaita-L Owner / Web Developer | | | -- | 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] | | | p.s: meu novo email é [EMAIL PROTECTED] . Christian Dechery (lemming) . http://www.tanamesa.com.br . Gaita-L Owner / Web Developer -- 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 multi-dimensional arrays
I'm having a little trouble sorting mult-dimensional arrays by key... I looked-up the manual and found out that array_multisort() thing... but I don't think it does what I want... or I don't understand it... I have an array like this... $categories=array(); $categories[0][id]=1; $categories[0][name]=Xyz; $categories[0][id]=7; $categories[0][name]=Lala; $categories[0][id]=2; $categories[0][name]=Lele; I want it sorted by name... the way I'm doing it right now is by using an auxiliar array that gets all the names, sorts them, and then build a new array based on that... is a better way? p.s: meu novo email é [EMAIL PROTECTED] . Christian Dechery (lemming) . http://www.tanamesa.com.br . Gaita-L Owner / Web Developer -- 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]
Re: [PHP] sorting multi-dimensional arrays
Try this (it should work): while(list($k, $v) = each($myarray)){ $myarray[$k] = (int) $v; } arsort($myarray); reset($myarray); while(list($k, $v) = each($myarray)){ echo $k, ' == ', $v, "BR\n; } For better comprehension look at this post: Hey guys.. I have this array that looks like this: $myarray = ( "apples" = 0, "oranges" = 2, "peaches" = 9, "pineapples" = 12, "stovetopstuffing" = 3, "grits" = 8); Fine and dandy. But, when I try to sort it in reverse order: arsort($myarray); I get THIS for output, obviously NOT what I want: peaches = 9 grits = 8 stovetopstuffing = 3 oranges = 2 pineapples = 12 apples = 0 Obviously, I want the pineapples entry to be at the top, since it has the highest value. What do I need to do? You have stored strings as your values, instead of ints. IE:(string) '9' (string) '12' returns TRUE You will need to take care in creating your array to have (int) values instead of strings. If all else fails, the following should work: while(list($k, $v) = each($myarray)){ $myarray[$k] = (int) $v; } arsort($myarray); reset($myarray); while(list($k, $v) = each($myarray)){ echo $k, ' == ', $v, "BR\n; } - Original Message - From: [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Saturday, April 14, 2001 5:19 AM Subject: [PHP] sorting multi-dimensional arrays I have a question about sorting multidimensional arrays. Here is my problem: I have an 2-d array: $joke[1][rating]=10; $joke[2][rating]=20; $joke[3][rating]=15; I would like to sort the jokes into an array based on the rating from highest to lowest. The end result would be something like this: Joke 2 : 20 points Joke 3: 15 points Joke 1: 10 points How do I accomplish this? I have tried fooling around with sort(), arsort(), and array_multisort(), but I just can't get it. Thank you very much for your time. Shane -- 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]
Re: [PHP] sorting multi-dimensional arrays
hh, no, maybe the samplecode I posted it's not your case, sorry... I have a question about sorting multidimensional arrays. Here is my problem: I have an 2-d array: $joke[1][rating]=10; $joke[2][rating]=20; $joke[3][rating]=15; I would like to sort the jokes into an array based on the rating from highest to lowest. The end result would be something like this: Joke 2 : 20 points Joke 3: 15 points Joke 1: 10 points How do I accomplish this? I have tried fooling around with sort(), arsort(), and array_multisort(), but I just can't get it. Thank you very much for your time. Shane -- 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]
Re: [PHP] sorting multi-dimensional arrays
The easiest way is probably to just use a for loop. For instance (no pun intended...well, maybe just a little one): $imax = sizeof($joke); for ($i = 0; $i $imax; $i++) { if (sizeof($joke[$i]) 1) { sort($joke[$i]); } } That will handle the sorting of any two-dimensional array. It won't effect the first dimension however, so if you want to sort that you will need to do it first. -- Plutarck Should be working on something... ...but forgot what it was. [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I have a question about sorting multidimensional arrays. Here is my problem: I have an 2-d array: $joke[1][rating]=10; $joke[2][rating]=20; $joke[3][rating]=15; I would like to sort the jokes into an array based on the rating from highest to lowest. The end result would be something like this: Joke 2 : 20 points Joke 3: 15 points Joke 1: 10 points How do I accomplish this? I have tried fooling around with sort(), arsort(), and array_multisort(), but I just can't get it. Thank you very much for your time. Shane -- 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]