PHP - 排序 ArrayObject
PHP - Sorting ArrayObject
我在对扩展 ArrayObject 的 PHP class 中的项目进行排序时遇到问题。
我正在创建我的 classes,我想出添加 cmp() 函数的唯一方法是将它放在同一个文件中,但在 class 之外.由于 uasort 需要将函数名称作为字符串的方式,我似乎无法将它放在其他任何地方。
所以我这样做了:
class Test extends ArrayObject{
public function __construct(){
$this[] = array( 'test' => 'b' );
$this[] = array( 'test' => 'a' );
$this[] = array( 'test' => 'd' );
$this[] = array( 'test' => 'c' );
}
public function sort(){
$this->uasort('cmp');
}
}
function cmp($a, $b) {
if ($a['test'] == $b['test']) {
return 0;
} else {
return $a['test'] < $b['test'] ? -1 : 1;
}
}
如果我像这样只使用一个 class 就没问题,但如果我使用两个(通过自动加载或 require)然后它会在尝试调用 cmp() 两次时中断。
我想我的意思是这似乎是一种糟糕的方法。有没有其他方法可以将 cmp()
函数保留在 class 本身中?
事实证明这在 PHP 文档(用户评论部分)中是正确的 -> http://php.net/manual/en/function.uasort.php
magikMaker 4 年前
如果您想在 Class 或对象中使用 uasort,请快速提醒语法:
<?php
// procedural:
uasort($collection, 'my_sort_function');
// Object Oriented
uasort($collection, array($this, 'mySortMethod'));
// Objet Oriented with static method
uasort($collection, array('self', 'myStaticSortMethod'));
?>
您可以这样做,而不是调用函数,只需将其设为匿名函数即可。
PHP 5.3.0 or higher only
class Test extends ArrayObject{
public function __construct(){
$this[] = array( 'test' => 'b' );
$this[] = array( 'test' => 'a' );
$this[] = array( 'test' => 'd' );
$this[] = array( 'test' => 'c' );
}
public function sort(){
$this->uasort(function($a, $b) {
if ($a['test'] == $b['test']) {
return 0;
} else {
return $a['test'] < $b['test'] ? -1 : 1;
}
});
}
}
由于匿名函数仅适用于 PHP 5.3.0 或更高版本,如果您需要针对低于 5.3.0PHP 的版本
,这将是更兼容的选项
Below PHP 5.3.0
class Test extends ArrayObject{
public function __construct(){
$this[] = array( 'test' => 'b' );
$this[] = array( 'test' => 'a' );
$this[] = array( 'test' => 'd' );
$this[] = array( 'test' => 'c' );
}
public function sort(){
$this->uasort(array($this, 'cmp'));
}
public function cmp($a, $b) {
if ($a['test'] == $b['test']) {
return 0;
} else {
return $a['test'] < $b['test'] ? -1 : 1;
}
}
}
我在对扩展 ArrayObject 的 PHP class 中的项目进行排序时遇到问题。
我正在创建我的 classes,我想出添加 cmp() 函数的唯一方法是将它放在同一个文件中,但在 class 之外.由于 uasort 需要将函数名称作为字符串的方式,我似乎无法将它放在其他任何地方。
所以我这样做了:
class Test extends ArrayObject{
public function __construct(){
$this[] = array( 'test' => 'b' );
$this[] = array( 'test' => 'a' );
$this[] = array( 'test' => 'd' );
$this[] = array( 'test' => 'c' );
}
public function sort(){
$this->uasort('cmp');
}
}
function cmp($a, $b) {
if ($a['test'] == $b['test']) {
return 0;
} else {
return $a['test'] < $b['test'] ? -1 : 1;
}
}
如果我像这样只使用一个 class 就没问题,但如果我使用两个(通过自动加载或 require)然后它会在尝试调用 cmp() 两次时中断。
我想我的意思是这似乎是一种糟糕的方法。有没有其他方法可以将 cmp()
函数保留在 class 本身中?
事实证明这在 PHP 文档(用户评论部分)中是正确的 -> http://php.net/manual/en/function.uasort.php
magikMaker 4 年前 如果您想在 Class 或对象中使用 uasort,请快速提醒语法:
<?php
// procedural:
uasort($collection, 'my_sort_function');
// Object Oriented
uasort($collection, array($this, 'mySortMethod'));
// Objet Oriented with static method
uasort($collection, array('self', 'myStaticSortMethod'));
?>
您可以这样做,而不是调用函数,只需将其设为匿名函数即可。
PHP 5.3.0 or higher only
class Test extends ArrayObject{
public function __construct(){
$this[] = array( 'test' => 'b' );
$this[] = array( 'test' => 'a' );
$this[] = array( 'test' => 'd' );
$this[] = array( 'test' => 'c' );
}
public function sort(){
$this->uasort(function($a, $b) {
if ($a['test'] == $b['test']) {
return 0;
} else {
return $a['test'] < $b['test'] ? -1 : 1;
}
});
}
}
由于匿名函数仅适用于 PHP 5.3.0 或更高版本,如果您需要针对低于 5.3.0PHP 的版本
,这将是更兼容的选项Below PHP 5.3.0
class Test extends ArrayObject{
public function __construct(){
$this[] = array( 'test' => 'b' );
$this[] = array( 'test' => 'a' );
$this[] = array( 'test' => 'd' );
$this[] = array( 'test' => 'c' );
}
public function sort(){
$this->uasort(array($this, 'cmp'));
}
public function cmp($a, $b) {
if ($a['test'] == $b['test']) {
return 0;
} else {
return $a['test'] < $b['test'] ? -1 : 1;
}
}
}