[英]Usort() expects parameter 2 to be a valid callback error in php7 - but works fine in php 5.6
So i have this sorting of multi-dimensional array:所以我有这种多维数组排序:
function score($somearray) {
$scores = 0;
if (($somearray[4] == '') AND (($somearray[5] == '') OR ($somearray[5] == '00:00:00')) AND (($somearray[6] == '') OR ($somearray[6] == '00:00:00'))) { // days are not set and time is not set
$scores = 1;
}
else if (($somearray[4] != '') AND (($somearray[5] == '') OR ($somearray[5] == '00:00:00')) AND (($somearray[6] == '') OR ($somearray[6] == '00:00:00'))) { // days are set and time is not set
$scores = 2;
}
else if (($somearray[4] == '') AND ((($somearray[5] != '') AND ($somearray[5] != '00:00:00')) OR (($somearray[6] != '') AND ($somearray[6] != '00:00:00')))) { // days are not set and time is set
$scores = 3;
}
else if (($somearray[4] != '') AND ((($somearray[5] != '') AND ($somearray[5] != '00:00:00')) OR (($somearray[6] != '') AND ($somearray[6] != '00:00:00')))) { // days are set and time is set
$scores = 4;
}
return $scores;
}
function cmp2(array $a, array $b)
{
return (score($b) - score($a));
}
$recordsarr = array();
$recordsarr[0] = ['1','5','2022-04-05','2022-04-15', '', '10:30:00', '11:30:00', '1', '800'];
$recordsarr[1] = ['1','5','2022-04-01','2022-04-20', '5', '10:30:00', '11:30:00', '1', '700'];
$recordsarr[2] = ['1','5','2022-04-05','2022-04-15', '4', '', '', '1', '800'];
$recordsarr[3] = ['1','0','2022-04-07','2022-04-15', '', '', '', '1', '800'];
$recordsarr[4] = ['1','5','2022-04-06','2022-04-16', '', '', '', '0', '900'];
$recordsarr[5] = ['1','5','2022-04-07','2022-04-12', '2', '', '', '0', '600'];
$majorpriorityarray = array();
if (count($recordsarr) > 0) {
usort($recordsarr, 'cmp2');
$majorpriorityarray = $recordsarr[0];
}
echo "major priority array is<br>";
var_dump($majorpriorityarray);
It works fine in php5.6, but does not work in php7, showing this error:在php5.6下可以正常使用,但是在php7下就不行了,报错:
usort() expects parameter 2 to be a valid callback, function 'cmp2' not found or invalid function name
usort() 期望参数 2 是一个有效的回调,function 'cmp2' not found or invalid function name
What i have to change in my code for working this sorting stuff in php7?我必须在我的代码中更改什么才能在 php7 中处理这些排序内容? Thanks
谢谢
UPD: Just tried: UPD:刚试过:
usort($recordsarr, function($a, $b) {
cmp2($a, $b);
});
Also tried:还尝试过:
function cmp2(array $a, array $b)
{
return score($b) <=> score($a);
}
Both these variants does not show an error, but are not sorting - do not affect the initial order.这两种变体都没有显示错误,但没有排序——不影响初始顺序。
Try to use尝试使用
usort($recordsarr, array( $this, "cmp2" ));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.