PHP Sort函数用于对对象数组进行排序 [英] PHP Sort function for sorting an array of objects
问题描述
我有一个数组,里面装满了来自同一类的对象.我想按可选对象字段(例如$case->ID
或$case->Sender
I have an array full of objects from the same class. I'd like to sort this array by optional object field, for instance $case->ID
or $case->Sender
是否已经内置了array_sort()函数,或者我必须自己编写此sort函数?
Is there a built in flavor of the array_sort() function that does this already, or will I have to write this sort function myself?
答案不必详细解释-这更像是是/否问题
Answer doesn't have to explain in detail - this is more like a yes/no question
谢谢
我对usort的尝试失败:
My failed attempt at usort:
function sortBy($sort)
{
usort($this->abuseCases, function($a, $b) {
if($a->{$sort} > $b->{$sort}) return 1;
if($a->{$sort} < $b->{$sort}) return -1;
else return 0;
});
}
另一次失败的尝试:
Another failed attempt:
function sortBy($sort)
{
$this->sortBy = $sort;
usort($this->abuseCases, array("this", "srt"));
}
private function srt($a, $b)
{
if($a->{$this->sortBy} > $b->{$this->sortBy}) return 1;
if($a->{$this->sortBy} < $b->{$this->sortBy}) return -1;
else return 0;
}
编辑凹凸
Edit for bump
推荐答案
您不仅可以使用匿名函数,还可以使用闭包,例如
You can use not only an anonymous function but a closure, like e.g.
function ($a,$b) use ($sort) { ... }
和$ sort将在功能主体中可用. 独立的示例:
and $sort will be available in the function body. Self-contained example:
<?php
function getFn($sort) {
return function($a, $b) use($sort) {
if($a->$sort > $b->$sort) return 1;
if($a->$sort < $b->$sort) return -1;
return 0;
};
}
$abuseCases = array(
(object)array('ID'=>1, 'Sender'=>'Sender A'),
(object)array('ID'=>3, 'Sender'=>'Sender D'),
(object)array('ID'=>2, 'Sender'=>'Sender C'),
(object)array('ID'=>4, 'Sender'=>'Sender B')
);
echo "\n----- Sort By Sender ----\n";
usort($abuseCases, getFn('Sender'));
foo($abuseCases);
echo "\n----- Sort By ID ----\n";
usort($abuseCases, getFn('ID'));
foo($abuseCases);
function foo($a) {
foreach($a as $o) {
echo $o->ID, ' ', $o->Sender, "\n";
}
}
打印
----- Sort By Sender ----
1 Sender A
4 Sender B
2 Sender C
3 Sender D
----- Sort By ID ----
1 Sender A
2 Sender C
3 Sender D
4 Sender B
更新:通过php< 5.3,您可以使用对象而不是匿名函数.
usort()期望第二个参数为callable
.自php 5.3起,这可能是一个异常的函数,但也可能是函数的名称....或作为数组(如array($obj, 'methodName')
)传递的对象和方法名称.
Update: With php<5.3 you can use an object instead of an anonymous function.
usort() expects the second parameter to be a callable
. That can be an anoymous function as of php 5.3, but it can also be the name of a function ....or an object and a method name passed as an array like array($obj, 'methodName')
.
$abuseCases = getData();
echo "\n----- Sort By Sender ----\n";
usort($abuseCases, array(new Foo('Sender'), 'compare'));
foo($abuseCases);
echo "\n----- Sort By ID ----\n";
usort($abuseCases, array(new Foo('ID'), 'compare'));
foo($abuseCases);
class Foo {
public $propName; // protected?
public function __construct($propertyName) {
$this->propName = $propertyName;
}
public function compare($a, $b) {
$prop = $this->propName;
if($a->$prop > $b->$prop) return 1;
if($a->$prop < $b->$prop) return -1;
return 0;
}
}
function foo($a) {
foreach($a as $o) {
echo $o->ID, ' ', $o->Sender, "\n";
}
}
function getData() {
return array(
(object)array('ID'=>1, 'Sender'=>'Sender A'),
(object)array('ID'=>3, 'Sender'=>'Sender D'),
(object)array('ID'=>2, 'Sender'=>'Sender C'),
(object)array('ID'=>4, 'Sender'=>'Sender B')
);
}
(如果您大量使用这个-或不想写这样的借口-_--您可能想定义一个像interface Comparator { ... }
这样的接口,让Foo实现该接口并具有一些函数/类使用Comparator,即usort()周围对象的包装器.)
(If you make heavy use of this - or don't want to write excuses like this one -_- - you might want to define an interface like interface Comparator { ... }
, let Foo implement that interface and have some function/class that uses Comparator, i.e. a wrapper for objects around usort().)
这篇关于PHP Sort函数用于对对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!