我想使用 uasort() 函数对数组进行排序。我写了这段代码并且运行良好:
function cmp($a, $b)
{
$_a = strlen($a);
$_b = strlen($b);
if($_a==$_b)
{
return 0;
}
return ($_a < $_b)? -1 : +1;
}
$arr = array(
"234560"=>"the quick brown fox jump",
"234561"=>"the quick brown fox jump over the lazy dog",
"234562"=>"the quick brown"
);
$array = uasort($arr,"cmp");
但是当我将它放入类中时,它返回 null 并带有警告:
Warning: uasort() expects parameter 2 to be a valid callback, function 'cmp' not found or invalid function name in...
这是我的代码
class Zend_Controller_Action_Helper_Utility extends Zend_Controller_Action_Helper_Abstract
{
public function uasort($array)
{
return uasort($array, "cmp");
}
function cmp($a, $b)
{
$_a = strlen($a);
$_b = strlen($b);
if ($_a == $_b)
{
return 0;
}
return ($_a < $_b) ? -1 : +1;
}
}
我也试试
return uasort($array, "Zend_Controller_Action_Helper_Utility::cmp");
使用静态函数 cmp(){}
但我仍然返回 null。
如何修复它?
最佳答案
您指定的回调错误。如果是实例方法,需要指定为
array($this, 'cmp');
如果将其设为静态方法,则需要将其指定为
array('Zend_Controller_Action_Helper_Utility', 'cmp');
请参阅documentation of callback了解更多详情。
关于php - 如何在 Zend Action Helper(或类中)中使用 uasort()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8458137/