php 多级数组按 su-sub-sub 数组值排序

php multilevel array sort by su-sub-sub array value

我有一个多级数组如下

array( 
    (int)0=>array(
              'User' => array(
        'PostType' => array(
            'PHP' => array(
                'id' => '2',
                'type_title' => 'Core Questions',
                'type_description' => 'none',
                'type_sort_order' => '7'
                'Post'=>array(
                    ......
                    ),
            ),
            'ASP' => array(
                'id' => '1',
                'type_title' => 'Core Questions',
                'type_description' => 'none',
                'type_sort_order' => '1'
                'Post'=>array(
                    ......
                    ),
            ),
        ),
)));

我获取了一个 post 分类为 postType
的用户 postType 有 type_sort_order 个字段
我想按 type_sort_order 字段
对子数组 PostType 进行排序 这样 ASPPHP
之前 我试过如下

usort($arr,function(){
                return  ($a[0]['User']['PostType']['post_sort_order'] < $b[0]['User']['PostType']['post_sort_order'])?1:-1;
            });

还有许多其他排序但没有得到正确的结果

您正在寻找http://php.net/manual/en/function.array-multisort.php

遍历 PostType 并将所有 type_sort_order 放入单独的数组中。然后将该数组用作 array_multi_sort

的参数

看来您只是想对子数组进行排序 PostType 所以只需传递该数组

array_multisort 会起作用。解决方法如下:

$list = //Your array

$sortOrders = array();
foreach ($list[0]['User']['PostType'] as $postType) {
    $sortOrders[] = $postType['type_sort_order'];
}

array_multisort($sortOrders, $list[0]['User']['PostType']);

这将抓取与您的起始数组排序相同的数组中的所有排序顺序。在两个数组上使用 array_multisort 将对第一个数组进行排序,并将新顺序应用于第二个数组,从而为您提供所需的结果。