Sorting an multidimensional array

Thread Solved

Join Date: Jun 2007
Posts: 231
Reputation: macneato is an unknown quantity at this point 
Solved Threads: 19
macneato's Avatar
macneato macneato is offline Offline
Posting Whiz in Training

Sorting an multidimensional array

 
0
  #1
Oct 22nd, 2009
Hi, so im pulling arrays from facebook. But i would like to sort them.

Here is how the arrays look
  1. Array
  2.  
  3. (
  4.  
  5. [0] => Array
  6.  
  7. (
  8.  
  9. [eid] => 203966119376
  10.  
  11. [name] => Funtastic - a show for the whole family!
  12.  
  13. [tagline] =>
  14.  
  15. [nid] => 0
  16.  
  17. [pic] => http://profile.ak.fbcdn.net/object2/857/53/s203966119376_6012.jpg
  18.  
  19. [pic_big] => http://profile.ak.fbcdn.net/object2/857/53/n203966119376_6012.jpg
  20.  
  21. [pic_small] => http://profile.ak.fbcdn.net/object2/857/53/t203966119376_6012.jpg
  22.  
  23. [host] => Silverstar Casino
  24.  
  25. [description] => Dates: 16 December 20097 March 2010
  26.  
  27. (Thurs – Sunday)
  28.  
  29. Times: Thurs, Fri and Sat at 8pm and Sundays at 3pm.
  30.  
  31. Book through Computicket
  32.  
  33.  
  34.  
  35. RSVP attending this event and stand a chance to win 2 tickets to the Funtastic Show.
  36.  
  37. [event_type] => Music/Arts
  38.  
  39. [event_subtype] => Performance
  40.  
  41. [start_time] => 1261022400
  42.  
  43. [end_time] => 1268020800
  44.  
  45. [creator] => 138907126863
  46.  
  47. [update_time] => 1256047911
  48.  
  49. [location] => Silverstar Centre
  50.  
  51. [venue] => Array
  52.  
  53. (
  54.  
  55. [street] =>
  56.  
  57. [city] =>
  58.  
  59. [state] =>
  60.  
  61. [country] =>
  62.  
  63. )
  64.  
  65.  
  66.  
  67. [privacy] => OPEN
  68.  
  69. [hide_guest_list] => 0
  70.  
  71. [show_in_search] => 0
  72.  
  73. )
  74.  
  75.  
  76.  
  77. [1] => Array
  78.  
  79. (
  80.  
  81. [eid] => 173559209746
  82.  
  83. [name] => Comedy Night with Joey Rasdien
  84.  
  85. [tagline] =>
  86.  
  87. [nid] => 0
  88.  
  89. [pic] => http://profile.ak.fbcdn.net/object2/1109/94/s173559209746_2253.jpg
  90.  
  91. [pic_big] => http://profile.ak.fbcdn.net/object2/1109/94/n173559209746_2253.jpg
  92.  
  93. [pic_small] => http://profile.ak.fbcdn.net/object2/1109/94/t173559209746_2253.jpg
  94.  
  95. [host] => Silverstar Casino
  96.  
  97. [description] => RSVP attending this event and stand a chance to win 2 tickets to the Comedy Night with Joey Rasdien at Silverstar Centre.
  98.  
  99. [event_type] => Music/Arts
  100.  
  101. [event_subtype] => Performance
  102.  
  103. [start_time] => 1256958000
  104.  
  105. [end_time] => 1256961600
  106.  
  107. [creator] => 138907126863
  108.  
  109. [update_time] => 1255943074
  110.  
  111. [location] => Silverstar Centre
  112.  
  113. [venue] => Array
  114.  
  115. (
  116.  
  117. [street] =>
  118.  
  119. [city] =>
  120.  
  121. [state] =>
  122.  
  123. [country] =>
  124.  
  125. )
  126.  
  127.  
  128.  
  129. [privacy] => OPEN
  130.  
  131. [hide_guest_list] => 0
  132.  
  133. [show_in_search] => 0
  134.  
  135. )
  136.  
  137.  
  138.  
  139. [2] => Array
  140.  
  141. (
  142.  
  143. [eid] => 141255783702
  144.  
  145. [name] => Steve Hofmeyr
  146.  
  147. [tagline] =>
  148.  
  149. [nid] => 0
  150.  
  151. [pic] => http://profile.ak.fbcdn.net/object2/575/107/s141255783702_3620.jpg
  152.  
  153. [pic_big] => http://profile.ak.fbcdn.net/object2/575/107/n141255783702_3620.jpg
  154.  
  155. [pic_small] => http://profile.ak.fbcdn.net/object2/575/107/t141255783702_3620.jpg
  156.  
  157. [host] => Silverstar Casino
  158.  
  159. [description] => RSVP attending this event and stand a chance to win 2 tickets to watch Steve Hofmeyr perform live Saturday, December 12th.
  160.  
  161. [event_type] => Music/Arts
  162.  
  163. [event_subtype] => Concert
  164.  
  165. [start_time] => 1260676800
  166.  
  167. [end_time] => 1260680400
  168.  
  169. [creator] => 138907126863
  170.  
  171. [update_time] => 1256035237
  172.  
  173. [location] => Silverstar Centre
  174.  
  175. [venue] => Array
  176.  
  177. (
  178.  
  179. [street] =>
  180.  
  181. [city] =>
  182.  
  183. [state] =>
  184.  
  185. [country] =>
  186.  
  187. )
  188.  
  189.  
  190.  
  191. [privacy] => OPEN
  192.  
  193. [hide_guest_list] => 0
  194.  
  195. [show_in_search] => 0
  196.  
  197. )

Then i display them using the following
  1. // check the result is loop-able
  2. if (is_array($events) && !empty($events)) {
  3.  
  4. $events = array_slice($events, 0,5);
  5.  
  6. foreach ($events as $event) {
  7.  
  8. echo "<fb:tag name='div'><fb:tag-attribute name='class'>event_image</fb:tag-attribute>
  9. <fb:tag-body>
  10. <fb:tag name='img'><fb:tag-attribute name='src'>".$event["pic"]."</fb:tag-attribute></fb:tag>
  11. </fb:tag-body>
  12. </fb:tag>";
  13. // Right Container
  14. echo "<fb:tag name='div'><fb:tag-attribute name='class'>event_details</fb:tag-attribute>
  15. <fb:tag-body>";
  16. echo "<fb:eventlink eid=".$event["eid"]." /><br/>";
  17. echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute>
  18. <fb:tag-body>
  19. <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute>
  20. <fb:tag-body>
  21. Type:
  22. </fb:tag-body>
  23. </fb:tag>
  24. ".$event["event_type"].
  25. "</fb:tag-body>
  26. </fb:tag>";
  27. echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute>
  28. <fb:tag-body>
  29. <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute>
  30. <fb:tag-body>
  31. When:
  32. </fb:tag-body>
  33. </fb:tag>
  34. <fb:date t='".$event["start_time"]."' format='monthname_time' tz='GMT+2' /></fb:tag-body></fb:tag>";
  35. echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute>
  36. <fb:tag-body>
  37. <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute>
  38. <fb:tag-body>
  39. Location:
  40. </fb:tag-body>
  41. </fb:tag> "
  42. .$event["location"]."</fb:tag-body></fb:tag>";
  43. echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute>
  44. <fb:tag-body>
  45. <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute>
  46. <fb:tag-body>
  47. Description:
  48. </fb:tag-body>
  49. </fb:tag> "
  50. .$event["description"]."</fb:tag-body>
  51. </fb:tag>";
  52. echo "</fb:tag-body></fb:tag>";
  53.  
  54. } // foreach
  55.  
  56. } // if

Heres my issue. I would like to sort the arrays by ["start_time"], so that when i display them, they are in order from soonest to latest.

Any help would be seriously appreciated. Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 557
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 64
network18 network18 is offline Offline
Posting Pro
 
0
  #2
Oct 22nd, 2009
use array_multisort($targetArray,SORT_ASC)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: deonheunis is an unknown quantity at this point 
Solved Threads: 1
deonheunis deonheunis is offline Offline
Newbie Poster

create_function

 
0
  #3
Oct 23rd, 2009
  1. function multi_sort($tab,$key){
  2. $compare = create_function('$a,$b','if ($a["'.$key.'"] == $b["'.$key.'"]) {return 0;}else {return ($a["'.$key.'"] > $b["'.$key.'"]) ? -1 : 1;}');
  3. usort($tab,$compare) ;
  4. return $tab ;
  5. }

This will do the job for you
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 231
Reputation: macneato is an unknown quantity at this point 
Solved Threads: 19
macneato's Avatar
macneato macneato is offline Offline
Posting Whiz in Training
 
0
  #4
Oct 23rd, 2009
Hi Deon,

Thanks for your help. Ending up going with the following, that works quite well

  1. // Get event arrays
  2. $today = time();
  3. $events = $facebook->api_client->events_get('138907126863','','$today','','');
  4.  
  5. // Sort arrays
  6. //Function used to sort array
  7. function csort($array, $column) {
  8. $s = array();
  9. foreach($array as $row) {
  10. $s[] = $row[$column];
  11. }
  12. array_multisort($s, SORT_ASC, SORT_NUMERIC, $array);
  13. return $array;
  14. }
  15. $events_sorted = csort($events, 'start_time');
  16.  
  17. // Slice sorted arrays
  18. $events_slice = array_slice($events_sorted, 0, 1);
  19.  
  20. // Used the sorted arrays to echo data
  21.  
  22. foreach ($events_slice as $event) {
  23. echo time();
  24. echo "<fb:tag name='div'><fb:tag-attribute name='class'>event_image</fb:tag-attribute>
  25. <fb:tag-body>
  26. <fb:tag name='img'><fb:tag-attribute name='src'>".$event["pic"]."</fb:tag-attribute></fb:tag>
  27. </fb:tag-body>
  28. </fb:tag>";
  29. echo "<fb:tag name='div'><fb:tag-attribute name='class'>event_details</fb:tag-attribute>
  30. <fb:tag-body>";
  31. echo "<fb:eventlink eid=".$event["eid"]." /><br/>";
  32. echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute>
  33. <fb:tag-body>
  34. <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute>
  35. <fb:tag-body>
  36. When:
  37. </fb:tag-body>
  38. </fb:tag>
  39. <fb:date t='".$event["start_time"]."' format='monthname_time' tz='GMT+2' /></fb:tag-body></fb:tag>";
  40.  
  41. }
If this reply solved your problem, please add to my reputation and don't forget to mark this thread as solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC