How can I sort results in PHP based on multiple conditions, including checking for empty fields?

To sort results in PHP based on multiple conditions, including checking for empty fields, you can use the `usort` function along with a custom comparison function. In the comparison function, you can define the logic to compare multiple fields and handle cases where fields are empty.

// Sample array of data to be sorted
$data = [
    ['name' => 'John', 'age' => 30],
    ['name' => 'Alice', 'age' => 25],
    ['name' => '', 'age' => 35],
    ['name' => 'Bob', 'age' => 28],
];

// Custom comparison function
usort($data, function($a, $b) {
    if ($a['name'] == '' && $b['name'] != '') {
        return 1; // Empty names go to the end
    } elseif ($a['name'] != '' && $b['name'] == '') {
        return -1; // Empty names go to the end
    } else {
        return $a['age'] <=> $b['age']; // Sort by age if names are not empty
    }
});

// Output sorted data
print_r($data);