php自定义排序

按照seq字段升序排列

<?php
    //将下面的数据内外皆排序
    $list = array(
        array(
            'seq' => 3,
            'data' => array(
                array(
                    'seq' => 2,
                    'desc' => '我的序号是内2'
                ),
                array(
                    'seq' => 1,
                    'desc' => '我的序号是内1'
                ),
                array(
                    'seq' => 3,
                    'desc' => '我的序号是内3'
                ),
            )
        ),
        array(
            'seq' => 1,
            'data' => array(
                array(
                    'seq' => 2,
                    'desc' => '我的序号是内2'
                ),
                array(
                    'seq' => 1,
                    'desc' => '我的序号是内1'
                ),
                array(
                    'seq' => 3,
                    'desc' => '我的序号是内3'
                ),
            )
        ),
        array(
            'seq' => 2,
            'data' => array(
                array(
                    'seq' => 2,
                    'desc' => '我的序号是内2'
                ),
                array(
                    'seq' => 1,
                    'desc' => '我的序号是内1'
                ),
                array(
                    'seq' => 3,
                    'desc' => '我的序号是内3'
                ),
            )
        )
    );
    function mySort($x, $y) {
        return $x['seq'] - $y['seq'];
    }
    foreach ($list as $key => $value) {
        usort($list[$key]['data'] ,'mySort');//注意只能不能将 $list[$key]['data'] 替换为 $value['data']
    }
    usort($list, 'mySort');
    echo json_encode($list);
?>

 

posted on 2017-04-29 14:39  码先生  阅读(335)  评论(0编辑  收藏  举报