简体   繁体   English

按字母排序多维数组不起作用?

[英]Sort Multidimensional Array Alphabetically not Working?

So I have an array called $links 所以我有一个名为$ 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()

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"]);'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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