从csv创建多维数组

时间:2011-09-30 05:40:15

标签: php

我无法从csv文件生成多维数组。我需要按国家/地区对输出进行“分组”,因为每个国家/地区可能有多个网络。某些行没有国家或区域的值,因为它们与上面的行相关。不幸的是,这是我收到csv文件的方式,没有办法改变输出。任何反馈或指示将不胜感激。

csv文件的片段......

Country|Zone|Network|Video|Voice
Afghanistan,5,Afghan Wireless,No,Yes
,,Roshan,No,Yes
Antigua,4,Digicel,No,Yes
Argentina,5,Telecom Personal,Yes,Yes
,,Movistar,No,Yes
,,Movistar2,Yes,Yes
Aruba,4,Digicel,No

理想输出

Array (
    [0] => Array (
        [country] => Afghanistan
        [zone] => 5
        [network] => Array (
            [0] => Array (
                [name] => Afghan Wireless
                [video] => No
                [voice] => Yes
            )
            [1] => Array (
                [name] => Roshan
                [video] => No
                [voice] => Yes
            )
        )
    )
    [1] => Array (
        [country] => Antigua
        [zone] => 4
        [network] => Array (
            [0] => Array (
                [name] => Digicell
                [video] => No
                [voice] => Yes
            )
        )
    )
    etc...
)

2 个答案:

答案 0 :(得分:2)

<?php 

$csvArray=array();//to store final data 
$networkArray=array();//to serve as temporar buffer

if (($handle = fopen("file.csv", "r")) !== FALSE) 
{
     fgetcsv($handle, 1000, ",");//skip the first line cause contains labels only
    //iterate all ther line 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    { 
        //if a new country
        if($data[0]!=='')
        {  
            /*get last key assigned to the previous country*/
            $key=array_pop(array_keys($csvArray)); 
            /*store the buffer, at the very begining no last country exists 
            so this network will be stored in a null key, will delete it later*/
            $csvArray[$key]['network']=$networkArray; 
            //emty the buffer
            $networkArray=array(); 
            //now we are done with previous country and will store the new one
            $csvArray[]=Array('country'=>$data[0],'zone'=>$data[1]);        
        }//@if ends 
            //Put to buffer network, video and voice
            $networkArray[]=Array('name'=>$data[2],'video'=>$data[3],'voice'=>$data[4]);     
    }//@while ends
    fclose($handle);
}//@outer if ends

//store the last network buffer
$key=array_pop(array_keys($csvArray));  
$csvArray[$key]['network']=$networkArray;   
//delete the null key set in the begining
array_shift($csvArray); 

//show the array
echo '<pre>'; 
print_r($csvArray); 

?>

答案 1 :(得分:0)

只是一个简单的解决方案(不是完整测试过的)

<?php

// Initialize the final result
$result = array();

// Read the file line by line
$handle = @fopen("test.csv", "r");
if ($handle) {
    $country = null;

    while (($buffer = fgets($handle, 4096)) !== false) {

        $line = explode(',', $buffer);

        if($line[0] == null) {

            $network['name'] = $line[2];
            $network['video'] = $line[3];
            if(isset($line[4])) $network['voice'] = $line[4];

            $country['network'][] = $network;

            $network = null;

        } else {
            if($country != null) {
                $result[] = $country;
                $country = null;
            }

            $country['country'] = $line[0];
            $country['zone'] = $line[1];

            $network['name'] = $line[2];
            $network['video'] = $line[3];
            if(isset($line[4])) $network['voice'] = $line[4];

            $country['network'][] = $network;

            $network = null;

        }

    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);

    print_r($result);
}