使用php按降序排序三级嵌套数组

时间:2013-11-07 20:42:36

标签: php arrays sorting multidimensional-array

如何按sorder降序排列以下数组?整个数组被认为是一个用于降序排序的数组。我看到其他一些问题,但没有一个对我有帮助。

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 2208
                    [status] => u13333333333333
                    [user_id] => 6
                    [sorder] => 3

                )

            [1] => Array
                (
                    [id] => 2208
                    [status] => user111111111111111
                    [user_id] => 6
                    [sorder] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 2209
                    [status] => u222222222222222222222
                    [user_id] => 5
                    [sorder] => 2

                )

        )

)

修改 也许这是二维数组的另一种形式。

Array
(
    [userPosts] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 2208
                            [status] => u13333333333333
                            [user_id] => 2208
                            [sorder] => 3
                        )

                    [1] => Array
                        (
                            [id] => 2208
                            [status] => user111111111111111
                            [user_id] => 2208
                            [sorder] => 1

                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 2209
                            [status] => u222222222222222222222
                            [user_id] => 2209
                            [sorder] => 2

                        )

                )

        )

)

2 个答案:

答案 0 :(得分:3)

$yourArray = array(

                array(
                        array('id' => 2208,
                                'status' => 'u13333333333333',
                                'user_id' => 6,
                                'sorder'=>3
                        ),
                        array('id' => 2208,
                                'status' => 'user111111111111111',
                                'user_id' => 6,
                                'sorder'=>1
                        ),
                ),
                array(
                        array('id' => 2209,
                                'status' => 'u222222222222222222222',
                                'user_id' => 5,
                                'sorder'=>2
                        ),

                ),


);
/*Merger arrays as one */
function loopArrayMerger(array $bigArray) {
    if (!$bigArray) {
        return array();
    }

    return call_user_func_array('array_merge', $bigArray);
}
$flatedArray = loopArrayMerger($yourArray);
/*Call back function for sorting, if you want in ascending order just replace 1:-1 with -1:1*/
function compareElementsInArray($a, $b) {
    if ($a['sorder'] == $b['sorder']) {
        return 0;
    }
    return ($a['sorder'] < $b['sorder']) ? 1 : -1;
}

/*Finally sort your array*/
uasort($flatedArray, 'compareElementsInArray');
echo '<pre>';
print_r($flatedArray);

答案 1 :(得分:0)

您想维护两个主阵列还是将它们合并为一个?这是一个解决方案,可以合并为一个类似的东西:

<?php

// merge branches of array
$array = array_merge($yourarray[0], $yourarray[1]);
$sorders = array();

foreach($array as $key => $current) {
    foreach($current as $subkey => $value) {
        // create new array with sorder as key, original key as value
        $sorders[$value['sorder']] = $subkey;   
    }
}

// sort by key (sorder)
ksort($sorders);

// create new array with ordered results
$new_array = array();
foreach($sorders as $sorder => $subkey) {

    // add values back
    $new_array[] = $array[0][$subkey];

}

print_r($new_array);

?>

另一个问题是这个数组最初是如何创建的?您可能会发现生成它的任何内容(如果它是某种框架)可能会在检索要按该字段排序的数据时提供选项。如果您使用MySQL自己的查询来获取它,可能最好在SQL查询中按该字段排序,而不是在...后执行此操作?