简体   繁体   English

按字母顺序排序多维数组

[英]Sort multidimensional array alphabetically

How can I sort a array like this alphabetically: 如何按字母顺序对数组进行排序:

$allowed = array(
  'pre'    => array(),
  'code'   => array(),
  'a'      => array(
                'href'  => array(),
                'title' => array()
              ),
  'strong' => array(),
  'em'     => array(),
);

// sort($allowed); ?

?

Aha! 啊哈! You need uksort(); 你需要uksort();

Comparison of PHP sorting functions. PHP排序函数的比较。 (dam useful) (大坝有用)

Edit: Reason is, you seem to want to sort inside arrays as well? 编辑:原因是,您似乎也希望在数组内部进行排序? AFAIK ksort by itself doesn't do that - it outright ignores the value of the original array. AFAIK ksort本身并不这样做 - 它完全忽略了原始数组的值。

Edit2: This ought to work (though uses recursion instead of kusort): Edit2:这应该工作(虽然使用递归而不是kusort):

function ksort_deep(&$array){
    ksort($array);
    foreach($array as &$value)
        if(is_array($value))
            ksort_deep($value);
}

// example of use:
ksort_deep($allowed);

// see it in action
echo '<pre>'.print_r($allowed,true).'</pre>';

Important: As a side effect of not using uksort() if the same array references to itself, you get an infinite loop. 重要说明:如果相同的数组引用自身,则不使用uksort()副作用是,您将获得无限循环。 This won't happen in normal cases, but you never know :) 这在正常情况下不会发生,但你永远不知道:)

ksort()

bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

as described here . 作为描述在这里 The 'See Also' section is usually very helpful “另请参阅”部分通常非常有用

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

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