0

如何按降序对以下数组进行排序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

                        )

                )

        )

)
4

2 回答 2

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);
于 2013-11-08T12:22:20.007 回答
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 查询中按该字段排序可能会更好,而不是在...之后执行它?

于 2013-11-07T20:52:00.860 回答