php - Sort array of objects using object's value -



php - Sort array of objects using object's value -

here's illustration array, $socialposts:

array( [18] => socialpost object ( [time] => 20140415 [url] => http://www.twitter.com/twitterapi [copy] => have agreed acquire @gnip, welcome flock! https://t.co/fxre36fjpz [image] => ) [19] => socialpost object ( [time] => 20140409 [url] => http://www.twitter.com/twitterapi [copy] => rt @twittersecurity: http://t.co/oobcosuknd & http://t.co/opmjvpbs6v servers not affected openssl vulnerability cve-2014-0160 http:… [image] => ) [20] => socialpost object ( [time] => 20140602 [url] => http://www.facebook.com/19292868552 [copy] => our june 2014 events calendar up! bring together @ events around world month, , larn how build, grow, , monetize apps facebook: https://developers.facebook.com/blog/post/2014/06/02/june-2014-developer-events-for-facebook-and-parse/ [image] => https://fbexternal-a.akamaihd.net/safe_image.php?d=aqbmv0yw8bcmcbmb&w=154&h=154&url=https%3a%2f%2ffbstatic-a.akamaihd.net%2frsrc.php%2fv2%2fy6%2fr%2fyqege6gxi_m.png ) )

i'm creating each socialpost object using info respective apis (twitter, facebook), , adding each socialpost object socialposts array.

i tried next rsort list them in reverse order [time] property:

function cmp($a, $b){ homecoming strcmp($a->time, $b->time); } rsort($socialposts, "cmp");

however, it's oddly plenty sorting twitter posts in right order, followed facebook posts in right order, in looks 2 separate sorts. should order them regardless source in right order according [time] value.

it's worth nil after adding each socialpost array, run loop format , update [time] property correctly, since twitter , facebook provided time detail in different formats. here's snippet well:

foreach($socialposts $socialpost){ $date_string = $socialpost->time; $date = new datetime($date_string); $date->settimezone(new datetimezone('america/new_york')); $formatted_date = $date->format('ymd'); $socialpost->time = $formatted_date; }

any ideas going wrong?

you should usort()

function cmp($a, $b) { homecoming strtotime($a->time) - strtotime($b->time); } usort($ar, "cmp");

if want reverse orders, alter cmp function to:

return strtotime($b->time) - strtotime($a->time);

which yields homecoming of:

array ( [0] => stdclass object ( [time] => 20140409 [url] => http://www.twitter.com/twitterapi [copy] => rt @twittersecurity: http://t.co/oobcosuknd & http://t.co/opmjvpbs6v servers not affected openssl vulnerability cve-2014-0160 http:รข€¦ [image] => ) [1] => stdclass object ( [time] => 20140415 [url] => http://www.twitter.com/twitterapi [copy] => have agreed acquire @gnip, welcome flock! https://t.co/fxre36fjpz [image] => ) [2] => stdclass object ( [time] => 20140602 [url] => http://www.facebook.com/19292868552 [copy] => our june 2014 events calendar up! bring together @ events around world month, , larn how build, grow, , monetize apps facebook: https://developers.facebook.com/blog/post/2014/06/02/june-2014-developer-events-for-facebook-and-parse/ [image] => https://fbexternal-a.akamaihd.net/safe_image.php?d=aqbmv0yw8bcmcbmb&w=154&h=154&url=https%3a%2f%2ffbstatic-a.akamaihd.net%2frsrc.php%2fv2%2fy6%2fr%2fyqege6gxi_m.png ) )

php arrays sorting object usort

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -