PHP: Natural sort array by a given key

Just a note to self, after running around in google search circles for a bit…

function array_sort_by($key, array &$array)
{
    return usort($array, function($x, $y) use ($key)
    {
        return strnatcasecmp($x[$key] ?? null, $y[$key] ?? null);
    });
}

Uses ?? null in case key doesn’t exist on some items.

Switch to strnatcmp if you want case-sensitive sorting.