Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组排序,如何将整个数组中的键1更改为键2_Php_Arrays_Sorting - Fatal编程技术网

Php 数组排序,如何将整个数组中的键1更改为键2

Php 数组排序,如何将整个数组中的键1更改为键2,php,arrays,sorting,Php,Arrays,Sorting,所以我有一个这样的数组: // 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_histor

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

//        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';
我试图阅读一些数组排序脚本,但我感到非常困惑,所以我实际上需要外部帮助。。。你们谁能给我一个线索吗

谢谢

$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
        )

)

StackOverflow不是这个问题的合适位置。我们不会为您编写代码。你需要自己编写代码,如果你不确定为什么有些东西不能按预期工作,那么在发布代码时解释一下你期望它做什么,以及它实际在做什么,包括所有错误消息。看,我不知道从哪里开始(从循环的
开始,它将通过
$historia[0][1]-$historia[x][0]
将它们添加到新数组中。然后循环第二个“$historia”将它们添加到新数组中。如果你是循环新手,从那里开始。