Reputation: 547
function sort_multi_array($array, $key)
{
if (is_null($array)) return 0;
$keys = array();
for ($i=1;$i<func_num_args();$i++) {
$keys[$i-1] = func_get_arg($i);
}
// create a custom search function to pass to usort
$func = function ($a, $b) use ($keys) {
for ($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 a simple search query however when it reaches the end i.e. no more entries in Warning: usort() expects parameter 1 to be array, null given in
How can I test to see if the array is empty and simply return a null result before it reaches the usort line?
thank you!
Upvotes: 0
Views: 1523
Reputation: 31749
Add a check for empty
data -
if (!empty($array)) {
// process data
}
Upvotes: 0
Reputation: 27072
Check before using usort
if the $array
is null or not.
if ($array !== NULL) {
usort($array, $func);
}
Upvotes: 2