使用多个条件排序多个数组

时间:2018-03-16 17:52:19

标签: php arrays sorting

我有这个数组:

array(
        '200' => array (
            "desc" => "sub array",
            "_id" => 200,
            "_parent" => 163,
            "_level" => 2,
            "_is_leaf" => false
        ),

        '165' => array (
            "desc" => "sub array from 200",
            "_id" => 165,
            "_parent" => 200,
            "_level" => 3,
            "_is_leaf" => true
        ),


        '167' => array (
            "desc" => "sub array from 172",
            "_id" => 167,
            "_parent" => 172,
            "_level" => 3,
            "_is_leaf" => true
        ),

        '163' => array (
            "desc" => "master array",
            "_id" => 163,
            "_parent" => null,
            "_level" => 1,
            "_is_leaf" => false
        ),

        '172' => array (
            "desc" => "sub array",
            "_id" => 172,
            "_parent" => 163,
            "_level" => 2,
            "_is_leaf" => false
        ),
    )

我的预期输出是

Master array
sub array 172
sub array from 172
sub array 200
sub array from 200

我试图像这样使用usort()

function sorting($a, $b)
{
    if ($a['_parent']==$b['_parent']) {
        return $a['_level'] - $b['_level'];
    }
    return $a['_id'] - $b['_id'];
}

但我仍然没有给出我预期的输出,有人能给我这个建议吗?

0 个答案:

没有答案