PHP usort二维数组不工作(按整数排序)

PHP usort二维数组不工作(按整数排序),php,arrays,sorting,Php,Arrays,Sorting,我有一个数组:$attachments=array() 其填充方式如下: if (have_rows('attachments_items')): while ( have_rows('attachments_items') ) : the_row(); $attachment_id = get_sub_field('fileattachment',false); $year = get_s

我有一个数组:
$attachments=array()

其填充方式如下:

if (have_rows('attachments_items')):
       while ( have_rows('attachments_items') ) : the_row();
                       $attachment_id = get_sub_field('fileattachment',false);
                       $year = get_sub_field('year',false);
                       if(is_null($year) || $year == 0){
                           $year=2018;
                       }
                   
                       $attachments[$i]['year'] = (int)$year;
                       $attachments[$i]['id'] = $attachment_id;
                       $i++;
      endwhile;
endif;
我想按年份对其进行排序,因此我尝试:

usort($attachments,function($first,$second){
    return $first->year < $second->year;
});
usort($attachments,function($first,$second){
返回$first->year<$second->year;
});
但它不起作用:

之前

未排序:数组(
[0]=>阵列([year]=>2018[id]=>14689)
=>阵列([年度]=>2017年[id]=>14690)
=>阵列([年度]=>2018年[id]=>14688)
[3] =>阵列([年度]=>2018年[id]=>14687)…)

之后

排序:数组(
[0]=>阵列([year]=>2018[id]=>14689)
=>阵列([年度]=>2018年[id]=>16323)
=>阵列([年度]=>2018年[id]=>21545)

[3] =>阵列([年度]=>2017年[id]=>14690)

[4] =>阵列([年度]=>2018年[id]=>12711) ……)


我认为您正在尝试对数组进行排序,但您将字段用作对象属性
$first->year

试用

usort($attachments, function ($first, $second) {
    return $first["year"] < $second["year"];
});

print_r($attachments);
usort($attachments,function($first,$second){
返回$first[“year”]<$second[“year”];
});
打印(附件);

为什么它不起作用?年是sorted@u_mulder它没有分类。请查看更新后的帖子。如果您打开了错误报告功能,您会注意到以下消息:
PHP注意:试图在中获取非对象的属性“year”…
()