What are some best practices for efficiently sorting and organizing data in PHP, especially when dealing with multiple fields and potential empty values?

When sorting and organizing data in PHP, especially when dealing with multiple fields and potential empty values, it is important to use a combination of array functions such as usort() and isset() to handle empty values and sort the data efficiently.

// Sample array with multiple fields and potential empty values
$data = [
    ['name' => 'John Doe', 'age' => 30],
    ['name' => 'Jane Smith', 'age' => null],
    ['name' => 'Alice Johnson', 'age' => 25],
];

// Sorting function to handle empty values
usort($data, function($a, $b) {
    if (!isset($a['age'])) {
        return 1;
    } elseif (!isset($b['age'])) {
        return -1;
    } else {
        return $a['age'] <=> $b['age'];
    }
});

// Output the sorted data
print_r($data);