您好我是PHP开发人员的新人
我尝试使用此帮助进行排序http://www.php.net/manual/en/function.usort.php#103722
function usortByArrayKey($key, $asc=SORT_ASC) {
$sort_flags = array(SORT_ASC, SORT_DESC);
if (!in_array($asc, $sort_flags))
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
return function(array $a, array $b) use ($key, $asc, $sort_flags) {
if (!is_array($key)) { //just one key and sort direction
if (!isset($a[$key]) || !isset($b[$key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$key] == $b[$key])
return 0;
return ($asc == SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
} else { //using multiple keys for sort and sub-sort
foreach ($key as $sub_key => $sub_asc) {
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if (!in_array($sub_asc, $sort_flags)) {
$sub_key = $sub_asc;
$sub_asc = $asc;
}
//just like above, except 'continue' in place of return 0
if (!isset($a[$sub_key]) || !isset($b[$sub_key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$sub_key] == $b[$sub_key])
continue;
return ($sub_asc == SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
};
}
问题是,我使用的是PHP 5.2 然后我像这样改变代码function usortByArrayKey(&$array, $key, $asc=SORT_ASC) {
$sort_flags = array(SORT_ASC, SORT_DESC);
if(!in_array($asc, $sort_flags)) {
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
}
$cmp = $this->_usortByArrayKey($key, $asc, $sort_flags);
usort($array, $cmp);
//return $array;
}
function _usortByArrayKey($key, $asc=SORT_ASC, array $sort_flags, array $a, array $b){
if(!is_array($key)) { //just one key and sort direction
if(!isset($a[$key]) || !isset($b[$key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$key] == $b[$key]) {
return 0;
}
return ($asc==SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
} else { //using multiple keys for sort and sub-sort
foreach($key as $sub_key => $sub_asc) {
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if(!in_array($sub_asc, $sort_flags))
{
$sub_key = $sub_asc; $sub_asc = $asc;
}
//just like above, except 'continue' in place of return 0
if(!isset($a[$sub_key]) || !isset($b[$sub_key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$sub_key] == $b[$sub_key]) {
continue;
}
return ($sub_asc==SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
}
但我仍然得到错误
错误:
Message: Argument 4 passed to ConnectList::_usortByArrayKey() must be an array, none given, called in C:\xampp\htdocs\receiverlist\application\models\connect.php on line 321 and defined
Uncaught exception 'Exception' with message 'attempting to sort on non-existent keys'
答案 0 :(得分:3)
在codeigniter中将回调函数输入PHP函数需要类似于:
usort($array, array('name-of-controller', 'name-of-method'));
显然在适当的地方更换名称。
答案 1 :(得分:2)
$cmp = $this->_usortByArrayKey($key, $asc, $sort_flags);
但是你的函数_usortByArrayKey
接受5个必需参数,而你只传递3.这就是第一条消息Message: Argument 4 passed to ConnectList::_usortByArrayKey() must be an array, none given, called in C:\xampp\htdocs\receiverlist\application\models\connect.php on line 321 and defined
出现的原因
答案 2 :(得分:0)
您要问的问题是您需要将PHP 5.2中没有的匿名函数转换为适用于PHP 5.2的函数。功能头:
function(array $a, array $b) use ($key, $asc, $sort_flags)
由于这是使用use
子句,您需要使用PHP 5.2中的类来模仿它。简化示例:
function ($a) use ($b)
课程可以这样:
class Func
{
private $b;
public function __construct($b)
{
$this->b = $b;
}
public function invoke($a) {
$b = $this->b; # operate on $b, not $this->b
}
public function getCallback() {
return array($this, 'invoke');
}
}
这是如何工作的?用法示例:
$func = new Func($b);
return $func->getCallback();
模式是什么?
use
子句中的变量是构造函数参数。invoke
函数。invoke
函数内,所有use-variables都被复制到本地范围。getCallback()
函数提供可用于调用函数的回调。