输出分组数组的所有结果

时间:2014-07-21 09:55:22

标签: php csv

我希望你能帮助我如何完成这个输出。

CSV文件

Department,Name,Employee No.,Date Time
LMS,"Bach, Jerome",102,6/30/2014 12:23
MTS,"Lorvia, Christine",103,6/16/2014 9:31
SSS,Jannah Curtis,104,6/16/2014 8:45
SSS,Jannah Curtis,104,6/28/2014 14:29 
ITM,Sassy Mica,105,6/17/2014 9:12 
ITM,Sassy Mica,105,6/17/2014 20:43
ITM,Sassy Mica,105,6/18/2014 9:12

我已将部门分组,这是输出

[MTS] => Array
    (
        [103] => Array
            (
                [0] => MTS
                [1] => Lorvia  Christine
                [2] => 103
                [3] => 6/16/2014 9:31
            )

    )

[SSS] => Array
    (
        [104] => Array
            (
                [0] => SSS
                [1] => Jannah Curtis
                [2] => 104
                [3] => 6/28/2014 14:29 
            )

    )

[ITM] => Array
    (
        [105] => Array
            (
                [0] => ITM
                [1] => Sassy Mica
                [2] => 105
                [3] => 6/18/2014 9:12
            )

    )

但我想要一个输出,它会在元素[3]下产生所有日期/时间记录。 防爆。

[ITM] => Array
        (
            [105] => Array
                (
                    [0] => ITM
                    [1] => Sassy Mica
                    [2] => 105
                    [3] => 6/17/2014 9:12
                        => 6/17/2014 20:43
                        => 6/18/2014 9:12
                )

        )

时间记录已排序。

1 个答案:

答案 0 :(得分:0)

你没有选择。您需要相应地处理它们。您想要的数组格式无效,因为它们不能共享相同的键,只需创建另一个维度。例如:

$data = array();
$handle = fopen('sample.csv', 'r');
while(!feof($handle)) {
    $row = fgetcsv($handle, '4096');
    if(!isset($data[$row[0]][$row[2]])) {
        // simple assignment
        $data[$row[0]][$row[2]] = array($row[0], $row[1], $row[2], array($row[3],));
    } else {
        // process
        $data[$row[0]][$row[2]][3][] = $row[3];  // push it inside instead of assigning a new one
        $temp = $data[$row[0]][$row[2]][3]; // temporary storage
        $temp = array_map(function($var){
            return strtotime($var); // convert to unix time
        }, $temp);
        sort($temp); // sort ascending
        $data[$row[0]][$row[2]][3] = array_map(function($var){
            return date('m/d/Y H:i', $var); // return to old format with sorted values
        }, $temp);


    }
}

array_shift($data); // remove the first (the header)
echo '<pre>';
print_r($data);

应该产生类似的东西:

Array
(
    [LMS] => Array
    (
        [102] => Array
        (
            [0] => LMS
            [1] => Bach, Jerome
            [2] => 102
            [3] => Array
            (
                [0] => 6/30/2014 12:23
            )
        )
    )

    [MTS] => Array
    (
        [103] => Array
        (
            [0] => MTS
            [1] => Lorvia, Christine
            [2] => 103
            [3] => Array
            (
                [0] => 6/16/2014 9:31
            )
        )
    )

    [SSS] => Array
    (
        [104] => Array
        (
            [0] => SSS
            [1] => Jannah Curtis
            [2] => 104
            [3] => Array
            (
                [0] => 06/16/2014 08:45
                [1] => 06/28/2014 14:29
            )
        )
    )

    [ITM] => Array
    (
        [105] => Array
        (
            [0] => ITM
            [1] => Sassy Mica
            [2] => 105
            [3] => Array
            (
                [0] => 06/17/2014 09:12
                [1] => 06/17/2014 20:43
                [2] => 06/18/2014 09:12
            )
        )
    )
)