Re: [PHP] Sorting an array
On 2/28/2011 7:52 PM, Ron Piggott wrote: > > I need help to know how to sort the words / phrases in my array. > > Variable name: $words_used > print_r( $words_used ); Current output: Array ( [187] => Sin [249] => > Punished [98] => Sanctuary [596] => Sing [362] => Anointing Oil ) > Desired result: Alphabetical sort: Array ( [362] => Anointing Oil [249] => > Punished [98] => Sanctuary [187] => Sin [596] => Sing ) > > The #’s are the auto_increment value of the word in the mySQL database. The > number is not representative of alphabetical order, but the order it was > added to the database. > > Thank you for your assistance. > > Ron > > The Verse of the Day > “Encouragement from God’s Word” > http://www.TheVerseOfTheDay.info > Besides the answer others have pointed you to, I would recommend doing it in mysql? SELECT * FROM ORDER BY ; Check out the syntax for it here. http://dev.mysql.com/doc/refman/5.0/en/select.html and http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html Jim Lucas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array
On 1/03/2011, at 4:52 PM, Ron Piggott wrote: > > I need help to know how to sort the words / phrases in my array. > > Variable name: $words_used > print_r( $words_used ); Current output: Array ( [187] => Sin [249] => > Punished [98] => Sanctuary [596] => Sing [362] => Anointing Oil ) > Desired result: Alphabetical sort: Array ( [362] => Anointing Oil [249] => > Punished [98] => Sanctuary [187] => Sin [596] => Sing ) > > The #’s are the auto_increment value of the word in the mySQL database. The > number is not representative of alphabetical order, but the order it was > added to the database. > > Thank you for your assistance. > > Ron > > The Verse of the Day > “Encouragement from God’s Word” > http://www.TheVerseOfTheDay.info Have a look at asort() (http://php.net/asort), which is used to sort arrays but maintain the key association. The default search option should work fine for you, if not try one of the other SORT_* flags described on the sort() manual page. --- Simon Welsh Admin of http://simon.geek.nz/ Who said Microsoft never created a bug-free program? The blue screen never, ever crashes! http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sorting an array
I need help to know how to sort the words / phrases in my array. Variable name: $words_used print_r( $words_used ); Current output: Array ( [187] => Sin [249] => Punished [98] => Sanctuary [596] => Sing [362] => Anointing Oil ) Desired result: Alphabetical sort: Array ( [362] => Anointing Oil [249] => Punished [98] => Sanctuary [187] => Sin [596] => Sing ) The #’s are the auto_increment value of the word in the mySQL database. The number is not representative of alphabetical order, but the order it was added to the database. Thank you for your assistance. Ron The Verse of the Day “Encouragement from God’s Word” http://www.TheVerseOfTheDay.info
Re: [PHP] Sorting an array of sub-arrays based on a sub-array's key
Eddie Drapkin wrote: > http://us3.php.net/uasort Exactly what I was looking for. Thanks. James -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array of sub-arrays based on a sub-array's key
On Sun, Sep 6, 2009 at 6:45 PM, James Colannino wrote: > Hey everyone. I have an array that looks like this: > > $main_array[0] = array('key1' => 'vala'); > $main_array[1] = array('key1' => 'valb'); > etc. > > I want to sort the main array based on the value of key1 for each > sub-array. I looked at all the array sorting functions, but unless I > misunderstood something, I didn't see a direct way to do what I want. > > If there were a sorting function in which I could pass as an argument > the name of a function that compares two elements like qsort in C, I > could do it easily. Is there a function like that in PHP? If not, what > should I do? > > Thanks everyone! > James > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > http://us3.php.net/uasort -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sorting an array of sub-arrays based on a sub-array's key
Hey everyone. I have an array that looks like this: $main_array[0] = array('key1' => 'vala'); $main_array[1] = array('key1' => 'valb'); etc. I want to sort the main array based on the value of key1 for each sub-array. I looked at all the array sorting functions, but unless I misunderstood something, I didn't see a direct way to do what I want. If there were a sorting function in which I could pass as an argument the name of a function that compares two elements like qsort in C, I could do it easily. Is there a function like that in PHP? If not, what should I do? Thanks everyone! James -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array..dont know numeric or alpha-numeric
This one time, at band camp, PHP Gen <[EMAIL PROTECTED]> wrote: > Hi, > I have a function that reads jpg files (thumbnails) > from a directory and puts all the files names into an > array...I want to sort that array by the filename, > problem is, I dont know if filenames will be pure > numeric (eg 001.jpg,002.jpg) or alpha-numeric > (asdf001,asdf002) '; } ?> or something. Kevin -- __ (_ \ _) ) | / / _ ) / _ | / ___) / _ ) | | ( (/ / ( ( | |( (___ ( (/ / |_| \) \_||_| \) \) Kevin Waterson Port Macquarie, Australia -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array..dont know numeric or alpha-numeric
Hi Curt, Damn, looks like I (unintentionally) gave you guys quite a challenge! You're the first one to reply and looks like you sure worked on it! > For starters, if your going to provide some code, > make sure its > readable by others.. trying to figure out what it is > doing is > nearly impossible the way it is written.. Sorry about that, I didnt write the code, I "snipped" it off from phpbuilder (i think, might have been another php help site) and since it was working I didnt touch or do anything. Will go through the modifications and advise you gave me and will write back if.. any problems. Thanks again, -Mag = -- - The faulty interface lies between the chair and the keyboard. - Creativity is great, but plagiarism is faster! - Smile, everyone loves a moron. :-) __ Do you Yahoo!? Yahoo! Mail - 50x more storage than other providers! http://promotions.yahoo.com/new_mail -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array..dont know numeric or alpha-numeric
* Thus wrote PHP Gen: >... > > Looking in the manual I have tried sort() without any > luck, then looking further I found natcasesort() which > would be perfect for my needs right now, but cant get > it to work :-( > > Below is the function (its not big) > > *** Start function > > function directory($dir,$filters){ > $handle=opendir($dir); > $files=array(); > if ($filters == "all"){while(($file = For starters, if your going to provide some code, make sure its readable by others.. trying to figure out what it is doing is nearly impossible the way it is written.. function directory($dir,$filters){ $handle=opendir($dir); $files=array(); if ($filters == "all"){ while(($file = readdir($handle)) !== false) { $files[] = $file; } } if ($filters != "all") { $filters=explode(",",$filters); while (($file = readdir($handle)) !== false) { for ($f=0;$f $pics=directory("pics","jpg,JPG,JPEG,jpeg,png,PNG"); > > *** End function > > I have tried putting natcasesort() in many places but > not working oh. well.. how were you trying natcasesort()? With the example provided in the manual: $array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png'); //... natcasesort($array2); print_r($array2); should help. Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sorting an array..dont know numeric or alpha-numeric
Hi, I have a function that reads jpg files (thumbnails) from a directory and puts all the files names into an array...I want to sort that array by the filename, problem is, I dont know if filenames will be pure numeric (eg 001.jpg,002.jpg) or alpha-numeric (asdf001,asdf002) It HAS to be sequential from small to big (eg: 0-10) as I am making static html pages from them further down the program. Looking in the manual I have tried sort() without any luck, then looking further I found natcasesort() which would be perfect for my needs right now, but cant get it to work :-( Below is the function (its not big) *** Start function function directory($dir,$filters){ $handle=opendir($dir); $files=array(); if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}} if ($filters != "all"){ $filters=explode(",",$filters); while (($file = readdir($handle))!==false) { for ($f=0;$fhttp://promotions.yahoo.com/new_mail -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array
Robert Cummings wrote: On Wed, 2004-06-02 at 12:59, John Nichel wrote: What I would like to do is sort this on the value of 'sort' in each sub-array. What am I missing TIA usort( $theAboveArray, 'mySortHandler' ); function mySortHandler( $v1, $v2 ) { if( $v1['sort'] > $v2['sort'] ) { return 1; } else if( $v1['sort'] < $v2['sort'] ) { return -1; } return 0; } Cheers, Rob. That did the trick. Thanks. -- John C. Nichel KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sorting an array
[snip] What I would like to do is sort this on the value of 'sort' in each sub-array. What am I missing TIA [/snip] http://us4.php.net/manual/en/function.array-multisort.php HTH! :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array
On Wed, 2004-06-02 at 12:59, John Nichel wrote: > Okay, I know someone is going to shoot me for asking such a dumb > question, but I just can't seem to find the answer anywhere. I have a > multidimensional array which I would like to sort on the value of one of > the keys... > > Array > ( > [0] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 5 > ) > [1] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 2 > ) > [2] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 4 > ) > [3] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 3 > ) > [4] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 1 > ) > ) > > What I would like to do is sort this on the value of 'sort' in each > sub-array. What am I missing TIA usort( $theAboveArray, 'mySortHandler' ); function mySortHandler( $v1, $v2 ) { if( $v1['sort'] > $v2['sort'] ) { return 1; } else if( $v1['sort'] < $v2['sort'] ) { return -1; } return 0; } 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 an array
Okay, I know someone is going to shoot me for asking such a dumb question, but I just can't seem to find the answer anywhere. I have a multidimensional array which I would like to sort on the value of one of the keys... Array ( [0] => Array ( [foo] => blah [bar] => blah [sort] => 5 ) [1] => Array ( [foo] => blah [bar] => blah [sort] => 2 ) [2] => Array ( [foo] => blah [bar] => blah [sort] => 4 ) [3] => Array ( [foo] => blah [bar] => blah [sort] => 3 ) [4] => Array ( [foo] => blah [bar] => blah [sort] => 1 ) ) What I would like to do is sort this on the value of 'sort' in each sub-array. What am I missing TIA -- John C. Nichel KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
Curt Zirzow wrote: > * Thus wrote Adam i Agnieszka Gasiorowski FNORD ([EMAIL PROTECTED]): > > Eugene Lee wrote: > > > > > On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD > > > wrote: > > > : > > > : There is an array of regexes, for example > > > : > > > : $array = array('moon', '[wh]ood', '[^as]eed' ... > > > : (about 300 entries). > > > : > > > : I want to sort it comparing to the > > > : character lenght of a regex. For example > > > : [wh]ood is 4 characters, moon is 4 characters. > > > : There are only letters of the alphabet and > > > : letter ranges present in those regexes. I > > > : want the "longest" ones first. > > > : > > > : How would you write the sorting function? > > > > > > This might be the most functionally correct, although it's definitely > > > not the fastest route. > > > > Thank you, that will certainly work :8]. Does > > anyone have any thoughts how to make it faster? It is > > not VERY critical, because the calculation will be > > done only once, at initialization, but...well, you > > know :8]. > > Do these change all the time or are they rather static? I would > suggest using this routine to generate the list to be used in your > program, instead of doing this on the fly. Good idea... -- Seks, seksić, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne kształty... http://www.opera.com 007 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
On Tue, Nov 18, 2003 at 01:52:39PM +0100, Wouter van Vliet wrote: : Eugene Lee suggested: : > On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka : > Gasiorowski FNORD wrote: : > : : > : There is an array of regexes, for example : > : : > : $array = array('moon', '[wh]ood', '[^as]eed' ... : > : (about 300 entries). : > : : > : I want to sort it comparing to the : > : character lenght of a regex. For example : > : [wh]ood is 4 characters, moon is 4 characters. : > : There are only letters of the alphabet and : > : letter ranges present in those regexes. I : > : want the "longest" ones first. : > : : > : How would you write the sorting function? : > : > This might be the most functionally correct, although it's definitely : > not the fastest route. : > : > function re_len($pat) : > { : > return strlen(preg_replace('/\[[^]]+]/', '_', $pat)); : : I think you meant: : : /\[[^\]]+]/ : : as regex ;) Not sure, but I think one more block-bracked needed to be : escaped ;) Nope. My pattern is legitimate. Within a range, if the first character is a closing-square-bracket ']', it is treated as the literal character and not as the end of range. If the range starts with a negation '^', then the same rule applies to the second character. This is also a sad indication that I really know my regular expressions, or I need a vacation. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
* Thus wrote Adam i Agnieszka Gasiorowski FNORD ([EMAIL PROTECTED]): > Eugene Lee wrote: > > > On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD wrote: > > : > > : There is an array of regexes, for example > > : > > : $array = array('moon', '[wh]ood', '[^as]eed' ... > > : (about 300 entries). > > : > > : I want to sort it comparing to the > > : character lenght of a regex. For example > > : [wh]ood is 4 characters, moon is 4 characters. > > : There are only letters of the alphabet and > > : letter ranges present in those regexes. I > > : want the "longest" ones first. > > : > > : How would you write the sorting function? > > > > This might be the most functionally correct, although it's definitely > > not the fastest route. > > Thank you, that will certainly work :8]. Does > anyone have any thoughts how to make it faster? It is > not VERY critical, because the calculation will be > done only once, at initialization, but...well, you > know :8]. Do these change all the time or are they rather static? I would suggest using this routine to generate the list to be used in your program, instead of doing this on the fly. Curt -- "My PHP key is worn out" PHP List stats since 1997: http://zirzow.dyndns.org/html/mlists/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
Eugene Lee wrote: > On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD wrote: > : > : There is an array of regexes, for example > : > : $array = array('moon', '[wh]ood', '[^as]eed' ... > : (about 300 entries). > : > : I want to sort it comparing to the > : character lenght of a regex. For example > : [wh]ood is 4 characters, moon is 4 characters. > : There are only letters of the alphabet and > : letter ranges present in those regexes. I > : want the "longest" ones first. > : > : How would you write the sorting function? > > This might be the most functionally correct, although it's definitely > not the fastest route. Thank you, that will certainly work :8]. Does anyone have any thoughts how to make it faster? It is not VERY critical, because the calculation will be done only once, at initialization, but...well, you know :8]. * * * How about...if I count the number of ']' in string and then add it to the strlen - (the number of ']' x 2)? There are no [ nor ] in these except in the character range parts. Does that look faster than applying regular expression? > function re_len($pat) > { > return strlen(preg_replace('/\[[^]]+]/', '_', $pat)); > } > > function re_sort($a_pat, $b_pat) > { > $a = re_len($a_pat); > $b = re_len($b_pat); > if ($a == $b) > { > return 0; > } > return ($a < $b ) ? -1 : 1; > } > > usort($array, 're_sort'); > > BTW, re_len() will bomb on certain oddball patterns with strange ranges. Like? Sorry, I can't think of anything right now... -- Seks, seksić, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne kształty... http://www.opera.com 007 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting an array of regexes
> -Oorspronkelijk bericht- > Van: Eugene Lee [mailto:[EMAIL PROTECTED] > On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka > Gasiorowski FNORD wrote: > : > : There is an array of regexes, for example > : > : $array = array('moon', '[wh]ood', '[^as]eed' ... > : (about 300 entries). > : > : I want to sort it comparing to the > : character lenght of a regex. For example > : [wh]ood is 4 characters, moon is 4 characters. > : There are only letters of the alphabet and > : letter ranges present in those regexes. I > : want the "longest" ones first. > : > : How would you write the sorting function? > > This might be the most functionally correct, although it's definitely > not the fastest route. > > function re_len($pat) > { > return strlen(preg_replace('/\[[^]]+]/', '_', $pat)); I think you meant: /\[[^\]]+]/ as regex ;) Not sure, but I think one more block-bracked needed to be escaped ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD wrote: : : There is an array of regexes, for example : : $array = array('moon', '[wh]ood', '[^as]eed' ... : (about 300 entries). : : I want to sort it comparing to the : character lenght of a regex. For example : [wh]ood is 4 characters, moon is 4 characters. : There are only letters of the alphabet and : letter ranges present in those regexes. I : want the "longest" ones first. : : How would you write the sorting function? This might be the most functionally correct, although it's definitely not the fastest route. function re_len($pat) { return strlen(preg_replace('/\[[^]]+]/', '_', $pat)); } function re_sort($a_pat, $b_pat) { $a = re_len($a_pat); $b = re_len($b_pat); if ($a == $b) { return 0; } return ($a < $b ) ? -1 : 1; } usort($array, 're_sort'); BTW, re_len() will bomb on certain oddball patterns with strange ranges. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sorting an array of regexes
There is an array of regexes, for example $array = array('moon', '[wh]ood', '[^as]eed' ... (about 300 entries). I want to sort it comparing to the character lenght of a regex. For example [wh]ood is 4 characters, moon is 4 characters. There are only letters of the alphabet and letter ranges present in those regexes. I want the "longest" ones first. How would you write the sorting function? -- Seks, seksić, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne kształty... http://www.opera.com 007 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array by value length.
On Wed, 22 Oct 2003 11:21:13 -0700, you wrote: >I've been scouring the php.net site for a few hours now trying to find a >viable way to sort a single dimensional array( array('0'=>'fddfsdsfdds', >'1'=>','d','2' => 'gofofle'); ). so that the longest lengthed >item is at the top, and the smallest lengthed item is at the bottom >is there a function to do this... example: $b) ? -1 : 1); } $a = array ('a', 'aa', 'aaa', '', 'a'); usort ($a, "cmp"); print_r ($a); ?> Standard warning: usort() has side-effects: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sorting an array by value length.
Hey all - I've been scouring the php.net site for a few hours now trying to find a viable way to sort a single dimensional array( array('0'=>'fddfsdsfdds', '1'=>','d','2' => 'gofofle'); ). so that the longest lengthed item is at the top, and the smallest lengthed item is at the bottom is there a function to do this... example: $arrayToSort = array('0' => 'shorter_length', '1' => 'longer_than_longer_than_shorter_length', '2' => 'longer_than_shorter_length'); and I would like to get... whether the keys change or not.. $arrayToSort[0] == 'longer_than_longer_than_shorter_length'; $arrayToSort[1] == 'longer_than_shorter_length'; $arrayToSort[2] == 'shorter_length'; any ideas? Rolf Brusletto www.phpExamples.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array
> > array[numrooms] => Array ( [3] => 2 [2] => 5 [1] => 1 ) > > I want to get a result like this: > > 2=>5 > > 3=>2 > > 1=>1 > > ( I want the keys to stay attached to the results) > > perhaps asort()? > "This function sorts an array such that array indices maintain their > correlation with the array elements they are associated with." Or arsort(), which will sort them in decending order. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array
perhaps asort()? "This function sorts an array such that array indices maintain their correlation with the array elements they are associated with." --- Diana Castillo <[EMAIL PROTECTED]> wrote: > Hi , I am trying to sort this: > array[numrooms] => Array ( [3] => 2 [2] => 5 [1] => 1 ) > I want to get a result like this: > 2=>5 > 3=>2 > 1=>1 > ( I want the keys to stay attached to the results) > > I used this to sort it : > array_multisort ($request_array['numrooms'], SORT_NUMERIC, > SORT_DESC); > > but instead I get this: > [numrooms] => Array ( [0] => 5 [1] => 2 [2] => 1 ) > > any suggestionis? > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > = Mark Weinstock [EMAIL PROTECTED] *** You can't demand something as a "right" unless you are willing to fight to death to defend everyone else's right to the same thing. *** __ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sorting an array
Hi , I am trying to sort this: array[numrooms] => Array ( [3] => 2 [2] => 5 [1] => 1 ) I want to get a result like this: 2=>5 3=>2 1=>1 ( I want the keys to stay attached to the results) I used this to sort it : array_multisort ($request_array['numrooms'], SORT_NUMERIC, SORT_DESC); but instead I get this: [numrooms] => Array ( [0] => 5 [1] => 2 [2] => 1 ) any suggestionis? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sorting an array
I am having problems sorting a multi-dimensional array. I am hoping that somebody can help me figure out what I am doing wrong. It should sort on the "field" than on the "fac". Here is my code: -start code- $sortterm; get_locations(); function cmp ($a, $b) { GLOBAL $sortterm; return strcmp($a[$sortterm], $b[$sortterm]); } function get_locations(){ GLOBAL $sortterm; $i="1"; while(($row=mysql_fetch_object($result))){ $locs[$i][loc_id]=$row->loc_id; $locs[$i][fac]=$row->fac; $locs[$i++][field]=$row->field; } reset($locs); $sortterm="field"; usort($locs, "cmp"); reset($locs); $sortterm="fac"; usort($locs, "cmp"); reset($locs); while (list ($key, $value) = each ($locs)) { echo $locs[$key][loc_id].", ".$locs[$key][fac].", ".$locs[$key][field]."\n"; } } -end code- Here is how it comes out when run: 12, Johnson County Community College, C 11, Johnson County Community College, B 13, Johnson County Community College, D 14, Johnson County Community College, E 10, Johnson County Community College, A 8, Johnson County Girls Athletic Assoc, D 6, Johnson County Girls Athletic Assoc, B 7, Johnson County Girls Athletic Assoc, C 5, Johnson County Girls Athletic Assoc, A 9, Shawnee Civic Center, 1 1, Tomahawk Sports Dome, 1 2, Tomahawk Sports Dome, 2 3, Tomahawk Sports Dome, 3 4, Tomahawk Sports Dome, 4 As you can see, the fac is sorted correctly, but the field isn't. Thanks Steve -- This message has been scanned for viruses and dangerous content by the MailScanner at ow5, and is believed to be clean. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array.
Sounds like it'd be a lot easier to let MySQL do the calculation and sorting. $userLatitude = 50; $userLongitude = 70; $sql = "select companyname, blah, blah, ceiling(sqrt(pow(69.1 * (latitude - $userLatitude), 2) + pow(53 * (longitude - $userLongitude), 2))) as distance from mytable order by distance"; miguel On Thu, 18 Apr 2002, Mike Mike wrote: > Here's what i'm working with. > I'm figuring the distance between to points based on > latitude and longitude. > > while ($row = mysql_fetch_array($result)) > { > //calculate distance between the two cities. > $Lat2 = $row["Latitude"]; > $Lon2 = $row["Longitude"]; > $x = 69.1 * ($Lat2 - $Lat1); > $y = 53 * ($Lon2 - $Lon1); > > $Distance = sqrt($x * $x + $y * $y); > $Distance = ceil($Distance); > >if ($Distance < 100){ > echo $row["CompanyName"].""; > echo $row["Address"].""; > echo $row["City"].""; > echo $row["State"].""; > echo $row["ZipCode"].""; > echo $row["PhoneNumber"].""; > echo $row["FaxNumber"].""; > > echo "Approximant Distance = $Distance > Miles"; > } > } > Here is where my question comes to play. How do I > sort the $Distance so that 4 Miles will come up first > 10 second 30 third ext.. > I have tried useing sort() function but it doesn't > work quite the way I want it to. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting an array.
Perhaps you could store $row in an array of arrays; store only $row within 100 miles. Add another field, Distance, to each row that holds the calculated distance. Then, after creating the array, sort the final array by Distance. - Original Message - From: Mike Mike <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, April 18, 2002 8:51 AM Subject: [PHP] Sorting an array. Hello, Here's what i'm working with. I'm figuring the distance between to points based on latitude and longitude. while ($row = mysql_fetch_array($result)) { file://calculate distance between the two cities. $Lat2 = $row["Latitude"]; $Lon2 = $row["Longitude"]; $x = 69.1 * ($Lat2 - $Lat1); $y = 53 * ($Lon2 - $Lon1); $Distance = sqrt($x * $x + $y * $y); $Distance = ceil($Distance); if ($Distance < 100){ echo $row["CompanyName"].""; echo $row["Address"].""; echo $row["City"].""; echo $row["State"].""; echo $row["ZipCode"].""; echo $row["PhoneNumber"].""; echo $row["FaxNumber"].""; echo "Approximant Distance = $Distance Miles"; } } Here is where my question comes to play. How do I sort the $Distance so that 4 Miles will come up first 10 second 30 third ext.. I have tried useing sort() function but it doesn't work quite the way I want it to. Thanks for your help --Mike __ Do You Yahoo!? Yahoo! Tax Center - online filing with TurboTax http://taxes.yahoo.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 an array.
Hello, Here's what i'm working with. I'm figuring the distance between to points based on latitude and longitude. while ($row = mysql_fetch_array($result)) { //calculate distance between the two cities. $Lat2 = $row["Latitude"]; $Lon2 = $row["Longitude"]; $x = 69.1 * ($Lat2 - $Lat1); $y = 53 * ($Lon2 - $Lon1); $Distance = sqrt($x * $x + $y * $y); $Distance = ceil($Distance); if ($Distance < 100){ echo $row["CompanyName"].""; echo $row["Address"].""; echo $row["City"].""; echo $row["State"].""; echo $row["ZipCode"].""; echo $row["PhoneNumber"].""; echo $row["FaxNumber"].""; echo "Approximant Distance = $Distance Miles"; } } Here is where my question comes to play. How do I sort the $Distance so that 4 Miles will come up first 10 second 30 third ext.. I have tried useing sort() function but it doesn't work quite the way I want it to. Thanks for your help --Mike __ Do You Yahoo!? Yahoo! Tax Center - online filing with TurboTax http://taxes.yahoo.com/ -- 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!
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. 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....
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....
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
[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
Re: [PHP] sorting an array of objects
On Wed, Oct 24, 2001 at 10:43:53AM +0100, L Melville wrote: > Is it possible to sort a class array using array multisort, I have two items > in each class that I wish to sort by, one is a text and the other is the > first item in an array within within each class. i.e.;- > > $class[$index]->text; > $class[$index]->array[0]; > > how can I do this? I'd use usort() instead. http://www.php.net/manual/en/function.usort.php The manual page has a couple examples for sorting objects by their member variables. Matt -- 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 an array of objects
Hi, Is it possible to sort a class array using array multisort, I have two items in each class that I wish to sort by, one is a text and the other is the first item in an array within within each class. i.e.;- $class[$index]->text; $class[$index]->array[0]; how can I do this? thanks in advance lee -- 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 an array
I'm trying to sort a listing of albums alphabetically for LyricsList.com, the problem is that I want the initial 'The' and 'A' in some albums (A Hard Days Night, The Wall) to be ignored. So that the list will look like: Abbey Road Dark Side Of the Moon The Final Cut A Hard Days Night Kill 'em All The Wall ABC order, 'A' and 'The' negated. The info comes from a database, in three calls: // call albums that begin with letter $letter $query = "SELECT * FROM albums WHERE album LIKE '$letter%' ORDER BY album"; $result = mysql_query($query) or die(mysql_error()); // call albums that begin with letter A $query_a = "SELECT * FROM albums WHERE album LIKE 'a' ORDER BY album"; $result_a = mysql_query($query_a) or die(mysql_error()); // call albums that begin with letter T $query_t = "SELECT * FROM albums WHERE album LIKE 't' ORDER BY album"; $result_t = mysql_query($query_t) or die(mysql_error()); So I need to get just the albums from $result_t that begin with 'The ' (easy regex) and from $result_a that begin with 'A ' (easy regex), but puting the three of them in order is proving beyond my skill. Thanks in advance for any advise. Dotan Cohen __ Terrorist Attacks on U.S. - How can you help? Donate cash, emergency relief information http://dailynews.yahoo.com/fc/US/Emergency_Information/ -- 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 an array
Nicklas af Ekenstam wrote: > > Hi! > > I have an array that looks kinda like this: > > NAMEADDRESS > John DoeSome Street 1 > Jane DoeAnother Street 2 > Bill Gates Helsinki 666 > > How do I sort this array based on NAME? ADDRESS? http://www.php.net/manual/en/function.usort.php -- Pavel a.k.a. Papi -- 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 an array
Hi! I have an array that looks kinda like this: NAMEADDRESS John DoeSome Street 1 Jane DoeAnother Street 2 Bill Gates Helsinki 666 How do I sort this array based on NAME? ADDRESS? Thanks!! - Nicklas -- 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 an array
http://www.php.net/manual/function.array-multisort.php Matthew Brealey wrote: > > I have a 2-d array: > > $list[$ctr]["username"] > $list[$ctr]["county"] > $list[$ctr]["age"] > $list[$ctr]["id"] > $list[$ctr]["days"] > > I need to sort by days, which is an integer. What function should I use? > > E.g., > $list[0][mark] > $list[0][somewhere] > $list[0][12] > $list[0][1] > $list[0][200] > > $list[1][andy] > $list[1][somewherefurther] > $list[1][18] > $list[1][2] > $list[1][100] > > $list[2][sam] > $list[2][somewhereelse] > $list[2][15] > $list[2][3] > $list[2][150] > > should sort to: > > $list[0][mark] > $list[0][somewhere] > $list[0][12] > $list[0][1] > $list[0][100] > > $list[1][andy] > $list[1][somewherefurther] > $list[1][18] > $list[1][2] > $list[1][150] > > $list[2][sam] > $list[2][somewhereelse] > $list[2][15] > $list[2][3] > $list[2][150] > -- Random (non-anti-Microsoft) fortune > > -- > 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] -- Monte Ohrt <[EMAIL PROTECTED]> http://www.ispi.net/ -- 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 an array
I have a 2-d array: $list[$ctr]["username"] $list[$ctr]["county"] $list[$ctr]["age"] $list[$ctr]["id"] $list[$ctr]["days"] I need to sort by days, which is an integer. What function should I use? E.g., $list[0][mark] $list[0][somewhere] $list[0][12] $list[0][1] $list[0][200] $list[1][andy] $list[1][somewherefurther] $list[1][18] $list[1][2] $list[1][100] $list[2][sam] $list[2][somewhereelse] $list[2][15] $list[2][3] $list[2][150] should sort to: $list[0][mark] $list[0][somewhere] $list[0][12] $list[0][1] $list[0][100] $list[1][andy] $list[1][somewherefurther] $list[1][18] $list[1][2] $list[1][150] $list[2][sam] $list[2][somewhereelse] $list[2][15] $list[2][3] $list[2][150] -- Random (non-anti-Microsoft) fortune -- 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]