I would like to sort an array of objects by a property of the specific object. This is my array with objects:
我想按特定对象的属性对对象数组进行排序。这是我的对象数组:
As you can see I have an array $all_studies
with 2 objects. How can I now sort on the graduationYear
property of the objects? So I would like to have an array with objects and the the order with object 2010 first, then 2014, ... (in this case the order is already correct but this won't always be the same ..).
如您所见,我有一个数组$all_studies,包含两个对象。我现在如何对对象的渐变年属性进行排序?所以我想要一个对象数组和对象2010的顺序,然后是2014。(在这种情况下,顺序已经是正确的,但这并不总是相同的。)
This is what I've tried but with no succes:
这是我试过的,但没有成功。
$all_studies = usort($all_studies, "sort_objects_by_graduationyear");
function sort_objects_by_graduationyear($a, $b) {
if((int)$a->graduationYear == (int)$b->graduationYear){ return 0 ; }
return ($a->graduationYear < $b->graduationYear) ? -1 : 1;
}
But I just get true
back. I've never used the usort function so I don't really know how to work with it. Can someone help me?
但我得到了真实的回应。我从来没有用过usort函数,所以我不知道怎么用它。有人能帮助我吗?
2 个解决方案
#1
3
The function usort returns "true" on success. So, good news :).
函数usort在成功时返回“true”。所以,好消息:)。
If you want to check if the sort is done, you only have to check your $all_studies object after the usort.
如果您想检查排序是否完成,您只需在usort之后检查$all_studies对象。
$status = usort($all_studies, "sort_objects_by_graduationyear");
print_r($all_studies);
#2
2
You were assigning the value of usort
to $all_studies
which'll be true
and false
thus you were not getting the value as desired. In fact you need to just sort
the array and print
that values and its all done
您将usort的值赋给了$all_studies,这是对的,也是错的,因此您没有得到想要的值。实际上,您需要对数组进行排序,并打印这些值,并将其全部完成。
Try as
试一试,
usort($all_studies, "sort_objects_by_graduationyear");
function sort_objects_by_graduationyear($a, $b) {
if((int)$a->graduationYear == (int)$b->graduationYear){ return 0 ; }
return ($a->graduationYear < $b->graduationYear) ? -1 : 1;
}
print_r($all_studies);
Return Values ¶
¶返回值
Returns TRUE on success or FALSE on failure.
成功时返回TRUE,失败时返回FALSE。
检查文档
#1
3
The function usort returns "true" on success. So, good news :).
函数usort在成功时返回“true”。所以,好消息:)。
If you want to check if the sort is done, you only have to check your $all_studies object after the usort.
如果您想检查排序是否完成,您只需在usort之后检查$all_studies对象。
$status = usort($all_studies, "sort_objects_by_graduationyear");
print_r($all_studies);
#2
2
You were assigning the value of usort
to $all_studies
which'll be true
and false
thus you were not getting the value as desired. In fact you need to just sort
the array and print
that values and its all done
您将usort的值赋给了$all_studies,这是对的,也是错的,因此您没有得到想要的值。实际上,您需要对数组进行排序,并打印这些值,并将其全部完成。
Try as
试一试,
usort($all_studies, "sort_objects_by_graduationyear");
function sort_objects_by_graduationyear($a, $b) {
if((int)$a->graduationYear == (int)$b->graduationYear){ return 0 ; }
return ($a->graduationYear < $b->graduationYear) ? -1 : 1;
}
print_r($all_studies);
Return Values ¶
¶返回值
Returns TRUE on success or FALSE on failure.
成功时返回TRUE,失败时返回FALSE。
检查文档