| | |
Sorting an multidimensional array
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi, so im pulling arrays from facebook. But i would like to sort them.
Here is how the arrays look
Then i display them using the following
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.
Here is how the arrays look
PHP Syntax (Toggle Plain Text)
Array ( [0] => Array ( [eid] => 203966119376 [name] => Funtastic - a show for the whole family! [tagline] => [nid] => 0 [pic] => http://profile.ak.fbcdn.net/object2/857/53/s203966119376_6012.jpg [pic_big] => http://profile.ak.fbcdn.net/object2/857/53/n203966119376_6012.jpg [pic_small] => http://profile.ak.fbcdn.net/object2/857/53/t203966119376_6012.jpg [host] => Silverstar Casino [description] => Dates: 16 December 2009 – 7 March 2010 (Thurs – Sunday) Times: Thurs, Fri and Sat at 8pm and Sundays at 3pm. Book through Computicket RSVP attending this event and stand a chance to win 2 tickets to the Funtastic Show. [event_type] => Music/Arts [event_subtype] => Performance [start_time] => 1261022400 [end_time] => 1268020800 [creator] => 138907126863 [update_time] => 1256047911 [location] => Silverstar Centre [venue] => Array ( [street] => [city] => [state] => [country] => ) [privacy] => OPEN [hide_guest_list] => 0 [show_in_search] => 0 ) [1] => Array ( [eid] => 173559209746 [name] => Comedy Night with Joey Rasdien [tagline] => [nid] => 0 [pic] => http://profile.ak.fbcdn.net/object2/1109/94/s173559209746_2253.jpg [pic_big] => http://profile.ak.fbcdn.net/object2/1109/94/n173559209746_2253.jpg [pic_small] => http://profile.ak.fbcdn.net/object2/1109/94/t173559209746_2253.jpg [host] => Silverstar Casino [description] => RSVP attending this event and stand a chance to win 2 tickets to the Comedy Night with Joey Rasdien at Silverstar Centre. [event_type] => Music/Arts [event_subtype] => Performance [start_time] => 1256958000 [end_time] => 1256961600 [creator] => 138907126863 [update_time] => 1255943074 [location] => Silverstar Centre [venue] => Array ( [street] => [city] => [state] => [country] => ) [privacy] => OPEN [hide_guest_list] => 0 [show_in_search] => 0 ) [2] => Array ( [eid] => 141255783702 [name] => Steve Hofmeyr [tagline] => [nid] => 0 [pic] => http://profile.ak.fbcdn.net/object2/575/107/s141255783702_3620.jpg [pic_big] => http://profile.ak.fbcdn.net/object2/575/107/n141255783702_3620.jpg [pic_small] => http://profile.ak.fbcdn.net/object2/575/107/t141255783702_3620.jpg [host] => Silverstar Casino [description] => RSVP attending this event and stand a chance to win 2 tickets to watch Steve Hofmeyr perform live Saturday, December 12th. [event_type] => Music/Arts [event_subtype] => Concert [start_time] => 1260676800 [end_time] => 1260680400 [creator] => 138907126863 [update_time] => 1256035237 [location] => Silverstar Centre [venue] => Array ( [street] => [city] => [state] => [country] => ) [privacy] => OPEN [hide_guest_list] => 0 [show_in_search] => 0 )
Then i display them using the following
PHP Syntax (Toggle Plain Text)
// check the result is loop-able if (is_array($events) && !empty($events)) { $events = array_slice($events, 0,5); foreach ($events as $event) { echo "<fb:tag name='div'><fb:tag-attribute name='class'>event_image</fb:tag-attribute> <fb:tag-body> <fb:tag name='img'><fb:tag-attribute name='src'>".$event["pic"]."</fb:tag-attribute></fb:tag> </fb:tag-body> </fb:tag>"; // Right Container echo "<fb:tag name='div'><fb:tag-attribute name='class'>event_details</fb:tag-attribute> <fb:tag-body>"; echo "<fb:eventlink eid=".$event["eid"]." /><br/>"; echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute> <fb:tag-body> <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute> <fb:tag-body> Type: </fb:tag-body> </fb:tag> ".$event["event_type"]. "</fb:tag-body> </fb:tag>"; echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute> <fb:tag-body> <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute> <fb:tag-body> When: </fb:tag-body> </fb:tag> <fb:date t='".$event["start_time"]."' format='monthname_time' tz='GMT+2' /></fb:tag-body></fb:tag>"; echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute> <fb:tag-body> <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute> <fb:tag-body> Location: </fb:tag-body> </fb:tag> " .$event["location"]."</fb:tag-body></fb:tag>"; echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute> <fb:tag-body> <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute> <fb:tag-body> Description: </fb:tag-body> </fb:tag> " .$event["description"]."</fb:tag-body> </fb:tag>"; echo "</fb:tag-body></fb:tag>"; } // foreach } // 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.
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 1
PHP Syntax (Toggle Plain Text)
function multi_sort($tab,$key){ $compare = create_function('$a,$b','if ($a["'.$key.'"] == $b["'.$key.'"]) {return 0;}else {return ($a["'.$key.'"] > $b["'.$key.'"]) ? -1 : 1;}'); usort($tab,$compare) ; return $tab ; }
This will do the job for you
0
#4 Oct 23rd, 2009
Hi Deon,
Thanks for your help. Ending up going with the following, that works quite well
Thanks for your help. Ending up going with the following, that works quite well
PHP Syntax (Toggle Plain Text)
// Get event arrays $today = time(); $events = $facebook->api_client->events_get('138907126863','','$today','',''); // Sort arrays //Function used to sort array function csort($array, $column) { $s = array(); foreach($array as $row) { $s[] = $row[$column]; } array_multisort($s, SORT_ASC, SORT_NUMERIC, $array); return $array; } $events_sorted = csort($events, 'start_time'); // Slice sorted arrays $events_slice = array_slice($events_sorted, 0, 1); // Used the sorted arrays to echo data foreach ($events_slice as $event) { echo time(); echo "<fb:tag name='div'><fb:tag-attribute name='class'>event_image</fb:tag-attribute> <fb:tag-body> <fb:tag name='img'><fb:tag-attribute name='src'>".$event["pic"]."</fb:tag-attribute></fb:tag> </fb:tag-body> </fb:tag>"; echo "<fb:tag name='div'><fb:tag-attribute name='class'>event_details</fb:tag-attribute> <fb:tag-body>"; echo "<fb:eventlink eid=".$event["eid"]." /><br/>"; echo "<fb:tag name='div'><fb:tag-attribute name='class'>text_standout</fb:tag-attribute> <fb:tag-body> <fb:tag name='div'><fb:tag-attribute name='class'>txt_event_heading</fb:tag-attribute> <fb:tag-body> When: </fb:tag-body> </fb:tag> <fb:date t='".$event["start_time"]."' format='monthname_time' tz='GMT+2' /></fb:tag-body></fb:tag>"; }
If this reply solved your problem, please add to my reputation and don't forget to mark this thread as solved.
![]() |
Similar Threads
- typedef or a multidimensional array? (C)
- Multidimensional array sort problem (C++)
- Question regarding multidimensional array for navigation bar (Graphics and Multimedia)
- Sort multidimensional array on more than one column? (ASP)
- multidimensional array merge using PHP (PHP)
- How to Sort a MultiDimensional Array (C)
- multidimensional array (C)
Other Threads in the PHP Forum
- Previous Thread: can you help a newbie?
- Next Thread: Why won't work?
| Thread Tools | Search this Thread |
.htaccess action ajax apache api array auto beginner binary bounce broken cakephp checkbox class cms code cron curl database date display dynamic echo email error errorlog file files folder form format forms function functions google href htaccess html image include insert integration interactive ip java javascript joomla limit link login loop mail malfunctioning masterthesis menu mlm mod_rewrite multiple mysql nodes oop paypal pdf php popup problem query radio ram random recursion reference regex remote return script search server sessions sms soap source space sql syntax system table tutorial unset update upload url validation validator variable video web websitecontactform xml youtube





