PHP 根据对象属性进行对象数组的排序(usort($your_data, "cmp");)(inside the class: usort($your_data, array($this, "cmp")))
PHP 根据对象属性进行对象数组的排序(usort($your_data, "cmp");
)(inside the class: usort($your_data, array($this, "cmp"))
)
usort($your_data, array($this, "cmp"))
一、总结
1、 usort($your_data, "cmp");
2、usort($your_data, array($this, "cmp")) If you’re sorting the array from inside the class and your sorting function cmp is also defined inside the class, then use this:
二、PHP 根据对象属性进行对象数组的排序
根据对象属性进行对象数组的排序【转自 stackoverflow 】
Sort array of objects by object fields
Question
How can I sort this array of objects by one of its fields, like name or count ?
Array
(
[0] => stdClass Object
(
[ID] => 1
[name] => Mary Jane
[count] => 420
)
[1] => stdClass Object
(
[ID] => 2
[name] => Johnny
[count] => 234
)
[2] => stdClass Object
(
[ID] => 3
[name] => Kathy
[count] => 4354
)
....
Answer :
Use usort
, here’s an example adapted from the manual:
function cmp($a, $b)
{
return strcmp($a->name, $b->name);
}
usort($your_data, "cmp");
edits imported from comments:
If you’re sorting the array from inside the class and your sorting function cmp is also defined inside the class, then use this:
usort($your_data, array($this, "cmp"))
版权申明:欢迎转载,但请注明出处
一些博文中有一些参考内容因时间久远找不到来源了没有注明,如果侵权请联系我删除。
在校每年国奖、每年专业第一,加拿大留学,先后工作于华东师范大学和香港教育大学。
2024-10-30:27岁,宅加太忙,特此在网上找女朋友,坐标上海,非诚勿扰,vx:fan404006308
AI交流资料群:753014672