欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

array中按照key排序的问题?

程序员文章站 2022-03-17 17:46:23
...
$test=array(60) {
 [0]=> object(stdClass)#14 (3) { ["id"]=> string(3) "609" ["title"]=> string(19) "Physical Mechanisms" ["publish_time"]=> string(10) "6 MAY 2016" }
 [1]=> object(stdClass)#14 (3) { ["id"]=> string(3) "610" ["title"]=> string(20) "Engineering Substrate" ["publish_time"]=> string(10) "2 MAY 2016" }
  ...}
 

根据publish_time对$test进行排序,怎么做?请教!

回复内容:

$test=array(60) {
 [0]=> object(stdClass)#14 (3) { ["id"]=> string(3) "609" ["title"]=> string(19) "Physical Mechanisms" ["publish_time"]=> string(10) "6 MAY 2016" }
 [1]=> object(stdClass)#14 (3) { ["id"]=> string(3) "610" ["title"]=> string(20) "Engineering Substrate" ["publish_time"]=> string(10) "2 MAY 2016" }
  ...}
 

根据publish_time对$test进行排序,怎么做?请教!

可以用php函数 usort 自定义排序

publish_time) publish_time) ) {
        return -1;
    } else if( strtotime($a->publish_time) > strtotime($a->publish_time) ){
        return 1;
    } else {
        return 0;
    }
}

usort($arr, '_usort');
相关标签: php arraylist