排序多维数组字母 [英] Sort multidimensional array alphabetically

查看:150
本文介绍了排序多维数组字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能进行排序这样一个按字母顺序排列:

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); ?

推荐答案

啊哈!您需要 uksort();

的PHP排序功能比较。(DAM有用)

编辑:原因是,你似乎想里面阵列以及排序? AFAIK ksort本身并不能做到这一点 - 它完全忽略了原始数组的值

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.

EDIT2:这应该工作(虽然使用递归代替kusort):

This ought to work (though uses recursion instead of 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>';

重要:由于不使用 uksort的副作用()如果本身相同数组引用,你会得到一个无限循环。这不会在正常的情况下发生,但你永远不知道:)

Important: As a side effect of not using uksort() if the same array references to itself, you get an infinite loop. This won't happen in normal cases, but you never know :)

这篇关于排序多维数组字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆