Current location: Hot Scripts Forums » Programming Languages » PHP » Sorting Array


Sorting Array

Reply
  #1 (permalink)  
Old 12-20-05, 05:20 AM
v1brazy v1brazy is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Newcastle - UK
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Question Sorting Array

Hya All,


I have an array like this:
Code:
$dan[] = array("date" => "12/01/2005","time" => "13:20");
$dan[] = array("date" => "11/01/2005","time"=> "14:20");
$dan[] = array("date" => "14/01/2005","time"=> "11:20");
asort($dan);
The outcome is:

Code:
Array ( [1] => Array ( [date] => 11/01/2005 [time] => 14:20 ) 
[0] => Array ( [date] => 12/01/2005 [time] => 13:20 ) 
[2] => Array ( [date] => 14/01/2005 [time] => 14:20 ) )
I would like to sort the array by "time":

I have tried asort($dan), but it sortes the array by date instead of time

Is there anyone that could help me?

Thanks everyone

Last edited by v1brazy; 12-20-05 at 05:26 AM. Reason: New Addition to code
Reply With Quote
  #2 (permalink)  
Old 12-20-05, 05:30 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
this a rather difficult function i think:
first - you'll have to make a loop to listen $dan
second - you'll have to store the values in 2 different arrays:
array1: the time as key and the $dan[key] as value
array2: the time as value
third - order array2
fourth - use array1 to reassign the ordered array2 to the $dan array

and after that you have sorted the $dan array

Good Luck i'ld say

OR you could switch "time" with "date" that way it'll sort the array by time instead of date, as it did earleir

Greetz,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks


Last edited by UnrealEd; 12-20-05 at 05:39 AM.
Reply With Quote
  #3 (permalink)  
Old 12-20-05, 10:46 AM
v1brazy v1brazy is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Newcastle - UK
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Thank You

Hya UnrealEd,

Thanks for the reply,

I have found a function in www.php.net and I have adapated it

Code:
//function to sort array by specified key
//This function was taken from http://uk.php.net/asort
//Customized and Commented by Daniel Hollis to suit my needs
function sort_outbound($array_tosort, $key_tosort) { 
   //loop thru array
   for ($i = 0; $i < sizeof($array_tosort); $i++) { 
       //add values of the array where $key_tosort is specified
	  $sort_values_array[$i] = $array_tosort[$i][$key_tosort]; 
   } 
   //sort array in ascending order
   asort ($sort_values_array); 
   
   //set pointer in the array to its first element
   reset ($sort_values_array); 
   
   //add to values to a sorted array  where the key is equal to the one specified
   while (list ($array_key, $array_value) = each ($sort_values_array)) { 
         $finalArray[] = $array_tosort[$array_key]; 
   } 
   return $finalArray; 
}
That did the trick, similar to what you have said,

Thanks again

Dan
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a massive multidimensional array dave111 PHP 1 12-12-05 04:09 PM
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 03:04 AM
file into an array, then appending array only if no duplicates djpennin Perl 2 05-17-04 01:04 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 06:23 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.