排序数组以导出到.csv文件 - PHP

时间:2013-12-17 21:40:36

标签: php

我正在尝试正确格式化我的数组,以便导出到.csv文件中,并且正确排序问题。用户将上传现有的.csv文件,这将获取数据并相应地使用。我正在尝试格式化它,因此索引中的名称是按字母顺序排列的。

以下是我使用以下内容将其打印到csv文件中时所获得的内容:

foreach($collectedData as $row) {
    if( !isset($normalized[$row[6]]) ) {
        $normalized[$row[6]] = array();
    }
    $normalized[$row[6]][] = $row;
}
print_r($normalized);

结果:

Array   
(   
    [Leonardo] => Array 
        (   
            [0] => Array    
                (   
                    [0] => 2    
                    [1] => 230  
                    [2] => 12 Aug 2013 07:46 AM 
                    [3] => 81680852 
                    [4] => INB  
                    [5] => 1.38 
                    [6] => Leonardo 
                )   

            [1] => Array    
                (   
                    [0] => 11   
                    [1] => 230  
                    [2] => 12 Aug 2013 08:35 AM 
                    [3] => 80411600 
                    [4] => INB  
                    [5] => 8.23 
                    [6] => Leonardo 
                )   

            [2] => Array    
                (   
                    [0] => 15   
                    [1] => 230  
                    [2] => 12 Aug 2013 09:03 AM 
                    [3] => 5122382  
                    [4] => INB  
                    [5] => 3.92 
                    [6] => Leonardo 
                )   

            [3] => Array    
                (   
                    [0] => 16   
                    [1] => 230  
                    [2] => 12 Aug 2013 09:09 AM 
                    [3] => 51275678 
                    [4] => INB  
                    [5] => 0.38 
                    [6] => Leonardo 
                )   

            [4] => Array    
                (   
                    [0] => 20   
                    [1] => 230  
                    [2] => 12 Aug 2013 10:06 AM 
                    [3] => 7367755  
                    [4] => INB  
                    [5] => 1.05 
                    [6] => Leonardo 
                )   

            [5] => Array    
                (   
                    [0] => 25   
                    [1] => 230  
                    [2] => 12 Aug 2013 10:22 AM 
                    [3] => 5831200  
                    [4] => INB  
                    [5] => 0.35 
                    [6] => Leonardo 
                )   

            [6] => Array    
                (   
                    [0] => 30   
                    [1] => 230  
                    [2] => 12 Aug 2013 11:25 AM 
                    [3] => 3333636  
                    [4] => INB  
                    [5] => 1.63 
                    [6] => Leonardo 
                )   

...........

这就是我想要的,因为它的格式是名字聚集在一起。但是当它被压缩到.csv文件中时它不起作用。

以下结果使用相同的方法,但使用了爆炸声明:

foreach($collectedData as $row) {
    if( !isset($normalized[$row[6]]) ) {
        $normalized[$row[6]] = array();

         echo implode("\t", array_keys($row)) . "\r\n";
    }
    $normalized[$row[6]][] = $row;
    echo implode("\t", array_values($row)) . "\r\n";
}

结果:

0   1   2   3   4   5   6
2   230 8/12/2013 7:46  8168670852  INB 1.38    Leonardo
11  230 8/12/2013 8:35  8045411600  INB 8.23    Leonardo
0   1   2   3   4   5   6
12  200 8/12/2013 8:44  8162155081  INB 0.52    Rick
15  230 8/12/2013 9:03  5176722382  INB 3.92    Leonardo
16  230 8/12/2013 9:09  5123175678  INB 0.38    Leonardo
...and so on

请注意里克如何使用莱昂纳多的名字?我的问题是如何正确格式化,以便print_r($normalized)结果与我将它们内嵌到.csv文件中的结果相同?

0 个答案:

没有答案