doumianfeng6979 2012-02-16 23:13
浏览 89
已采纳

如何通过子数组值对多维数组进行分组并在PHP中重新分配键

I looked at all the similar questions and samples (there are many) but still can't get this to work (though I think I am close).

I have an array of employee schedules pulled from a text file...

schedule.txt:


    123,Joe,20120208,0845,1645
    123,Joe,20120209,0800,1600
    456,Sue,20120208,0900,1700
    456,Sue,20120209,0700,1500
    ...

My code to generate the array is:


    $schedule = file_get_contents('./schedule.txt');
    $s = explode("
", $schedule);
    for ($i = 0; $i < count($s); $i++) {
        $s[$i] = explode(",", $s[$i]);
    }
    print_r($s);

Output is:


    Array
    (
        [0] => Array
            (
                [0] => 123
                [1] => Joe
                [2] => 20120208
                [3] => 0845
                [4] => 1645
            )

        [1] => Array
            (
                [0] => 123
                [1] => Joe
                [2] => 20120209
                [3] => 0800
                [4] => 1600
            )

        [2] => Array
            (
                [0] => 456
                [1] => Sue
                [2] => 20120208
                [3] => 0900
                [4] => 1700
            )

        [3] => Array
            (
                [0] => 456
                [1] => Sue
                [2] => 20120209
                [3] => 0700
                [4] => 1500
            )
    )

I am instead trying to group employees by Id in a new multidimensional array with output like this:


    Array
    (
        [123] => Array
            (
                [0] => Array
                    (
                        [0] => Joe
                        [1] => 20120208
                        [2] => 0845
                        [3] => 1645
                    )

                [1] => Array
                    (
                        [0] => Joe
                        [1] => 20120209
                        [2] => 0800
                        [3] => 1600
                    )
            )

        [456] => Array
            (
                [0] => Array
                    (
                        [0] => Sue
                        [1] => 20120208
                        [2] => 0900
                        [3] => 1700
                    )

                [1] => Array
                    (
                        [0] => Sue
                        [1] => 20120209
                        [2] => 0700
                        [3] => 1500
                    )
            )
    )

I cannot for the life of me wrap my head around how to change my existing array into this new (different) output... I am trying this but getting nowhere:


    $newArr = array();

    foreach($s as $k => $v) {
        if(!isset($newArr[$v[0]])) {
            $newArr[$v[0]] = array();
        }

        $newArr[$v[0]][] = array($v[0]);
    }

Though my output is now:


    Array
    (
        [123] => Array
            (
                [0] => Array
                    (
                        [0] => 123
                    )

                [1] => Array
                    (
                        [0] => 123
                    )
            )

        [456] => Array
            (
                [0] => Array
                    (
                        [0] => 456
                    )

                [1] => Array
                    (
                        [0] => 456
                    )
            )
    )

Any thoughts? I know I am missing something in my code around the line:


    $newArr[$v[0]][] = array($v[0]);

I tried changing it to:


    $newArr[$v[0]][] = array(0 => $v[1], 1 => $v[2], 2 => $v[3], 3 => $v[4]);

which gives my the right output, but I now get undefined offset errors. Plus, if the number of keys in the sub array changes, this code is now limited to the 4 I explicitly entered...

  • 写回答

4条回答 默认 最新

  • douhun8647 2012-02-16 23:18
    关注

    this should be enough, if I understood you right :)

    $lines = file('./schedule.txt');
    
    $employees = array();
    foreach ($lines as $line)
    {
        $chunks = explode(',', $line);
        $employees[$chunks[0]][] = array_slice($chunks, 1);
    }
    
    print_r($employees);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试