按所有级别上的键对关联多维数组排序



我有以下数组:

$data['uploads']['ccc'] = 'something';
$data['uploads']['bbb'] = 'something';
$data['uploads']['aaa'] = 'something';
$data['downloads']['ccc'] = 'something';
$data['downloads']['aaa'] = 'something';
$data['downloads']['bbb'] = 'something';

我需要它按键字母顺序排序,所以最终结果需要是:

$data['downloads']['aaa'] = 'something';
$data['downloads']['bbb'] = 'something';
$data['downloads']['ccc'] = 'something';
$data['uploads']['aaa'] = 'something';
$data['uploads']['bbb'] = 'something';
$data['uploads']['ccc'] = 'something';

尝试了各种自定义排序函数,没有运气…任何想法?

ksort($data);
http://php.net/manual/en/function.ksort.php

同样来自PHP文档中的注释:

下面是一个按键递归排序多维数组的函数:

function deep_ksort(&$arr) { 
    ksort($arr); 
    foreach ($arr as &$a) { 
        if (is_array($a) && !empty($a)) { 
            deep_ksort($a); 
        } 
    } 
} 

相关内容

  • 没有找到相关文章

最新更新