简体   繁体   中英

Sort Multidimensional Array Alphabetically not Working?

So I have an array called $links

array(
    [0] = array(
        'type' => 'thread'
        'url' => 'blah blah blah'
    ),
    [1] = array(
        'type' => 'media'
        'url' => 'blah blah blah'
    ),
    [2] = array(
        'type' => 'website'
        'url' => 'blah blah blah'
    )
);

What I am trying to do is sort this array alphabetically using "type". For this I am using usort()

usort($links, create_function('$b, $a', 'return $a["type"] - $b["type"];'));

The problem is, this is not actually sorting the array... all it does is REVERSE the array. After running through, I get Website > Media > Thread. If I process it a second time, it reverses back to Thread > Media > Website.

The final result should be Media > Thread > Website. Am I missing something? Why is this not sorting correctly?

试试这个,代替:

usort($links, create_function('$a, $b', 'return strcmp($a["type"], $b["type"]);'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM