Re: [PHP] Sort an array by its values
Do you mean something like www.php.net/ksort or www.php.net/krsort? I'm not sure of the actual structure of your array, though, so those might not work as expected. /* Chris Lambert, CTO - [EMAIL PROTECTED] WhiteCrown Networks - More Than White Hats Web Application Security - www.whitecrown.net */ - Original Message - From: Aaron Bennett [EMAIL PROTECTED] To: Php-General (E-mail) [EMAIL PROTECTED] Sent: Sunday, July 08, 2001 4:25 AM Subject: [PHP] Sort an array by its values | Hi All, | I'm trying to sort an array of objects by the value of one of those | objects... | for instance, I'll have an object with 2 properties, id and score, where | id is unique and score is its relevant (and sometimes simelar) score. i've | tried using sort() and asort() but i can't figure how to base the sorting | off the value (i.e. score) of the object in the array. Any help? | -- | Aaron | -- 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] Sort an array by its values
Chris, I'm looking at ksort as i type... the problem i'm having is that i'm trying to sort a value inside an object inside that array (rather than paired values in an assoc. array) -- Aaron -Original Message- From: Chris Lambert - WhiteCrown Networks [mailto:[EMAIL PROTECTED]] Sent: Sunday, July 08, 2001 1:33 AM To: Aaron Bennett Cc: [EMAIL PROTECTED] Subject: Re: [PHP] Sort an array by its values Do you mean something like www.php.net/ksort or www.php.net/krsort? I'm not sure of the actual structure of your array, though, so those might not work as expected. /* Chris Lambert, CTO - [EMAIL PROTECTED] WhiteCrown Networks - More Than White Hats Web Application Security - www.whitecrown.net */ - Original Message - From: Aaron Bennett [EMAIL PROTECTED] To: Php-General (E-mail) [EMAIL PROTECTED] Sent: Sunday, July 08, 2001 4:25 AM Subject: [PHP] Sort an array by its values | Hi All, | I'm trying to sort an array of objects by the value of one of those | objects... | for instance, I'll have an object with 2 properties, id and score, where | id is unique and score is its relevant (and sometimes simelar) score. i've | tried using sort() and asort() but i can't figure how to base the sorting | off the value (i.e. score) of the object in the array. Any help? | -- | Aaron |
Re: [PHP] Sort an array by its values
Can you print_r($variable) and post it here? /* Chris Lambert, CTO - [EMAIL PROTECTED] WhiteCrown Networks - More Than White Hats Web Application Security - www.whitecrown.net */ - Original Message - From: Aaron Bennett [EMAIL PROTECTED] To: 'Chris Lambert - WhiteCrown Networks' [EMAIL PROTECTED]; Aaron Bennett [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Sunday, July 08, 2001 4:34 AM Subject: RE: [PHP] Sort an array by its values | Chris, | I'm looking at ksort as i type... the problem i'm having is that i'm | trying to sort a value inside an object inside that array (rather than | paired values in an assoc. array) | -- | Aaron | | -Original Message- | From: Chris Lambert - WhiteCrown Networks [mailto:[EMAIL PROTECTED]] | Sent: Sunday, July 08, 2001 1:33 AM | To: Aaron Bennett | Cc: [EMAIL PROTECTED] | Subject: Re: [PHP] Sort an array by its values | | | Do you mean something like www.php.net/ksort or www.php.net/krsort? | | I'm not sure of the actual structure of your array, though, so those might | not work as expected. | | /* Chris Lambert, CTO - [EMAIL PROTECTED] | WhiteCrown Networks - More Than White Hats | Web Application Security - www.whitecrown.net | */ | | - Original Message - | From: Aaron Bennett [EMAIL PROTECTED] | To: Php-General (E-mail) [EMAIL PROTECTED] | Sent: Sunday, July 08, 2001 4:25 AM | Subject: [PHP] Sort an array by its values | | | | Hi All, | | I'm trying to sort an array of objects by the value of one of those | | objects... | | for instance, I'll have an object with 2 properties, id and score, | where | | id is unique and score is its relevant (and sometimes simelar) score. i've | | tried using sort() and asort() but i can't figure how to base the sorting | | off the value (i.e. score) of the object in the array. Any help? | | -- | | Aaron | | | -- 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] Sort an array by its values
I think array_multisort is supposed to do this but I couldn't get it to work. Have a look at the usort call in the following routine; // Reads a directory and puts filenames into a array sorted by date function GetFiles ($DataPath, $files, $nfiles) { $handle=@opendir($DataPath); if ($handle) { $nfiles = 0; while (false!==($file = readdir($handle))) { if ($file != . $file != .. ) { $files[$nfiles][name] = $file; $files[$nfiles][date] = filemtime($DataPath.\\.$file); $nfiles++; } } if($nfiles1){ usort($files, create_function( '$a,$b', 'return $a[date]==$b[date]?0:($a[date]$b[date]?1:-1);') ); } closedir($handle); } } HTH Andrew Braund -Original Message- From: Aaron Bennett [mailto:[EMAIL PROTECTED]] Sent: Sunday, 8 July 2001 17:56 To: Php-General (E-mail) Subject: [PHP] Sort an array by its values Hi All, I'm trying to sort an array of objects by the value of one of those objects... for instance, I'll have an object with 2 properties, id and score, where id is unique and score is its relevant (and sometimes simelar) score. i've tried using sort() and asort() but i can't figure how to base the sorting off the value (i.e. score) of the object in the array. Any help? -- Aaron -- 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] Sort an array by its values
Chris, Due to the amount of HTML contained within the object, it would be nearly impossible to decipher from a print_r() the way it was... I was able to make an assoc. array with the id as the key and score as value, so i get something more like: Array ( [29] = 152 [13] = 98 [58] = 134 [6] = 66 [9] = 66 [15] = 68 [18] = 86 [49] = 66 [50] = 104 [51] = 66 [52] = 66 [59] = 100 [61] = 68 [66] = 68 ) which i should be able to use uasort() and array_reverse() to order them and maintain the correct index... Thanks for the help, i think i got it from here. -- Aaron -Original Message- From: Chris Lambert - WhiteCrown Networks [mailto:[EMAIL PROTECTED]] Sent: Sunday, July 08, 2001 1:38 AM To: Aaron Bennett Cc: [EMAIL PROTECTED] Subject: Re: [PHP] Sort an array by its values Can you print_r($variable) and post it here? /* Chris Lambert, CTO - [EMAIL PROTECTED] WhiteCrown Networks - More Than White Hats Web Application Security - www.whitecrown.net */ - Original Message - From: Aaron Bennett [EMAIL PROTECTED] To: 'Chris Lambert - WhiteCrown Networks' [EMAIL PROTECTED]; Aaron Bennett [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Sunday, July 08, 2001 4:34 AM Subject: RE: [PHP] Sort an array by its values | Chris, | I'm looking at ksort as i type... the problem i'm having is that i'm | trying to sort a value inside an object inside that array (rather than | paired values in an assoc. array) | -- | Aaron | | -Original Message- | From: Chris Lambert - WhiteCrown Networks [mailto:[EMAIL PROTECTED]] | Sent: Sunday, July 08, 2001 1:33 AM | To: Aaron Bennett | Cc: [EMAIL PROTECTED] | Subject: Re: [PHP] Sort an array by its values | | | Do you mean something like www.php.net/ksort or www.php.net/krsort? | | I'm not sure of the actual structure of your array, though, so those might | not work as expected. | | /* Chris Lambert, CTO - [EMAIL PROTECTED] | WhiteCrown Networks - More Than White Hats | Web Application Security - www.whitecrown.net | */ | | - Original Message - | From: Aaron Bennett [EMAIL PROTECTED] | To: Php-General (E-mail) [EMAIL PROTECTED] | Sent: Sunday, July 08, 2001 4:25 AM | Subject: [PHP] Sort an array by its values | | | | Hi All, | | I'm trying to sort an array of objects by the value of one of those | | objects... | | for instance, I'll have an object with 2 properties, id and score, | where | | id is unique and score is its relevant (and sometimes simelar) score. i've | | tried using sort() and asort() but i can't figure how to base the sorting | | off the value (i.e. score) of the object in the array. Any help? | | -- | | Aaron | | |
Re: [PHP] Sort an array by its values
Try something like: uasort($your_hash[$first_level][$any_second_level], create_function('$a,$b', 'return $a[what_level] - $b[what_level];')); and adjust this according to the level and what you want to sort on accordingly... _jef Aaron Bennett wrote: Hi All, I'm trying to sort an array of objects by the value of one of those objects... for instance, I'll have an object with 2 properties, id and score, where id is unique and score is its relevant (and sometimes simelar) score. i've tried using sort() and asort() but i can't figure how to base the sorting off the value (i.e. score) of the object in the array. Any help? -- Aaron -- Justin Farnsworth Eye Integrated Communications 321 South Evans - Suite 203 Greenville, NC 27858 | Tel: (252) 353-0722 -- 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]