我需要知道如何通过'post_title'对这个PHP数组进行排序

时间:2016-01-20 21:14:03

标签: php arrays wordpress

我正在使用wordpress主题,并且我的一个页面上有一个数组填充了内容,“$chunks

以下是$chunks的初始化方式:

$chunks = array_chunk($args['exhibitors'], 5);

我尝试使用以下代码进行排序,但它不起作用。

function sortByOrder($a, $b) {
return $a['post_title'] - $b['post_title'];
}

usort($chunks, 'sortByOrder');

我认为我不了解如何访问'post_title'中的字符串。

vardump $chunks以下(对于数组中的第一个条目,因此您可以看到格式):

array(1) { 
   [0]=> array(5) {  
           [0]=> object(WP_Post)#538 (24) { 
                 ["ID"]=>int(466) 
                 ["post_author"]=> string(1) "1" 
                 ["post_date"]=> string(19) "2016-01-20 20:46:50"
                 ["post_date_gmt"]=> string(19) "2016-01-20 20:46:50" 
                 ["post_content"]=> string(866) "For more than three 
                  decades, we have developed speakers and electronics to meet the demanding 
                  requirements of professional applications. But we also create 
                  technological advancements like Modeler® design software, the 
                  Auditioner® audio demonstrator and LT loudspeaker technology. These 
                  tools and innovations enable designers and consultants to create more 
                  accurate, reliable, and cost-effective solutions. Bose® Professional 
                  Systems is an entire division of engineers, trainers, technical 
                  specialists and sales support teams. We provide support to a worldwide 
                  network of dealers and installers. It includes product and technical 
                  information, marketing and demonstration materials, design resources 
                  and other materials and services. We help our dealers and installers 
                  take projects from start to finish and provide a high level of service 
                  after the job is done." 
                  ["post_title"]=> string(4) "Bose"
                  ["post_excerpt"]=> string(0) "" 
                  ["post_status"]=> string(7) "publish"
                  ["comment_status"]=> string(6) "closed"  
                  ["ping_status"]=> string(6) "closed" 
                  ["post_password"]=> string(0) "" 
                  ["post_name"]=> string(4) "bose" 
                  ["to_ping"]=> string(0) "" 
                  ["pinged"]=> string(0) ""
                  ["post_modified"]=> string(19) "2016-01-20 20:46:57"
                  ["post_modified_gmt"]=> string(19) "2016-01-20 20:46:57"
                  ["post_content_filtered"]=> string(0) "" 
                  ["post_parent"]=> int(0)
                  ["guid"]=> string(55) "XXXXX" 
                  ["menu_order"]=> int(0) 
                  ["post_type"]=> string(9) "exhibitor" 
                  ["post_mime_type"]=> string(0) ""
                  ["comment_count"]=> string(1) "0" 
                  ["filter"]=> string(3) "raw" 
           }

1 个答案:

答案 0 :(得分:1)

我认为你需要在分块之前对数组进行排序。

$exhibitors = $args['exhibitors'];
usort($exhibitors, function($a, $b) {
    if ($a->post_title > $b->post_title) return 1;
    if ($a->post_title < $b->post_title) return -1;
    return 0;
});

然后你可以对排序的数组进行分块:

$chunks = array_chunk($exhibitors, 5);

如果我对sort / chunk的顺序错了,这个比较函数应该仍然可以工作。

请记住,usort的比较函数需要返回一个整数,可以是负数,正数或零,具体取决于比较结果。

中减去一个post_title
return $a['post_title'] - $b['post_title'];

很可能会返回0,因为这些字符串将被转换为减法数字,除非它们恰好以数字开头,否则你将有效地获得

0 - 0 = 0

因此根本没有排序,基本上是