2011-09-30 143 views
1

我无法从csv文件生成多维数组。我需要按国家的输出“分组”,因为每个国家可能有多个网络。有些行没有国家或地区的值,因为它们与其上方的行相关。不幸的是,这是我如何收到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
<?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); 

?> 
+0

非常感谢你,工作过。而且我还学习了更多关于数组和循环的知识。 – Jamie

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); 
}