0

所以我有一个像这样的数组:

//        user 1 $historia[id_usuario][id_historia]
    $historia[1][1] = '1asdfasf';
    $historia[1][8] = '1asdfasf';
    $historia[1][7] = '1asdfasf';
    $historia[1][4] = '1asdfasf';

//        user 2 $historia[id_usuario][id_historia]
    $historia[2][5] = '2asdfasf';
    $historia[2][6] = '2asdfasf';
    $historia[2][3] = '2asdfasf';
    $historia[2][2] = '2asdfasf';

我需要让它们按第二个键排序,最终结果应该是这样的:

    //person1
    $historia[1][1] = '1asdfasf';
    $historia[8][1] = '1asdfasf';
    $historia[7][1] = '1asdfasf';
    $historia[4][1] = '1asdfasf';

    //person2
    $historia[5][2] = '2asdfasf';
    $historia[6][2] = '2asdfasf';
    $historia[3][2] = '2asdfasf';
    $historia[2][2] = '2asdfasf';

接着!

    $historia[1][1] = '1asdfasf';
    $historia[2][2] = '2asdfasf';
    $historia[3][2] = '2asdfasf';
    $historia[4][1] = '1asdfasf';
    $historia[5][2] = '2asdfasf';
    $historia[6][2] = '2asdfasf';
    $historia[7][1] = '1asdfasf';
    $historia[8][1] = '1asdfasf';

我试图阅读一些数组排序脚本,但我变得非常困惑,所以我实际上需要外部帮助......你们中的任何人都可以给我一个领导吗?

谢谢!

4

1 回答 1

0
$historia[1][1] = '1asdfasf';
$historia[1][8] = '1asdfasf';
$historia[1][7] = '1asdfasf';
$historia[1][4] = '1asdfasf';
$historia[2][5] = '2asdfasf';
$historia[2][6] = '2asdfasf';
$historia[2][3] = '2asdfasf';
$historia[2][2] = '2asdfasf';

$new_array=array();
foreach($historia as $uid=>$sub)
{
    foreach($sub as $hid=>$val)
    {
        if(empty($new_array[$hid])) $new_array[$hid]=array();
        $new_array[$hid][$uid]=$val;
    }
}
ksort($new_array);
print_r($new_array);

输出:

Array
(
    [1] => Array
        (
            [1] => 1asdfasf
        )

    [2] => Array
        (
            [2] => 2asdfasf
        )

    [3] => Array
        (
            [2] => 2asdfasf
        )

    [4] => Array
        (
            [1] => 1asdfasf
        )

    [5] => Array
        (
            [2] => 2asdfasf
        )

    [6] => Array
        (
            [2] => 2asdfasf
        )

    [7] => Array
        (
            [1] => 1asdfasf
        )

    [8] => Array
        (
            [1] => 1asdfasf
        )

)
于 2013-01-28T04:01:27.933 回答