Sort in PHP

Hello, guys. Can u please help me with sorting in php? I cant sort out how to sort this thing from a to z.
I tried using sort, but i think i dont understand how array works or idk. Thanks for any help.

It looks like you want to sort the array, but you want to keep the array keys you’re assigning in line 81.

If that’s the case, use asort. This function will sort the array in place but keep the product ids correctly lined up:

$list = [1 => 'charlie', 2 => 'able', 3 => 'baker'];

The above code will produce the output:

    [2] => able
    [3] => baker
    [1] => charlie

Note that the values are now sorted alphabetically, but the keys remains attached to their original values.

This kind of operation is a hangover of PHP’s slightly weird approach to arrays; most languages will not allow you to sort a list of key => value pairs in this way.

Thank you very much. That’s exactly what i needed :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service