PHP USort gives null warning when array is empty -
function sort_multi_array($array, $key) { if (is_null($array)) return 0; $keys = array(); ($i=1;$i<func_num_args();$i++) { $keys[$i-1] = func_get_arg($i); } // create custom search function pass usort $func = function ($a, $b) use ($keys) { ($i=0;$i<count($keys);$i++) { if ($a[$keys[$i]] != $b[$keys[$i]]) { return ($a[$keys[$i]] > $b[$keys[$i]]) ? -1 : 1; } } return 0; }; usort($array, $func); return $array; } i'm building simple search query when reaches end i.e. no more entries in warning: usort() expects parameter 1 array, null given in
how can test see if array empty , return null result before reaches usort line?
thank you!
check before using usort if $array null or not.
if ($array !== null) { usort($array, $func); }
Comments
Post a Comment