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:
usort() expects parameter 2 to be a valid callback, function 'cmp2' not found or invalid function name
What i have to change in my code for working this sorting stuff in php7? Thanks
UPD: Just tried:
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