2014-06-24 132 views
0

我正在使用PHP打開並解析一個非常小(約1kb)的CSV文件以生成HTML表。我是PHP新手,這在很大程度上是實驗性的。除了生成HTML表格,我試圖從該csv(城市和國家信息)中的一組特定列生成數組,然後刪除重複的值。的CSV的結構是這樣的:從PHP數組獲取特定的CSV列或第n個值

Last Name, First Name, City, Country, Language 
Smith, Joe, Shanghai, China, English 
Jackson, Stacey, Madrid, Spain, Spanish 
Jones, Bob, London, United Kingdom, English 
Seward, Elisa, Madrid, Spain, English 
Harrison, Tim, Berlin, Germany, German 

這裏的想法是,除了所有的數據表,我還必須在表中列出的所有城市/國家名單:

  • 上海,中國
  • 馬德里,西班牙
  • 倫敦,英國
  • 柏林,德國

得益於fgetcsv()文檔和堆棧溢出的其他問題,閱讀文件和構建表很簡單:

<?php 
    $handle = fopen("namelist.csv", "r"); 
    $data = fgetcsv($handle, 1000, ","); 
    echo('<table>'); 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     echo("<tr>\r\n"); 
     foreach ($data as $index=>$val) { 
      echo("\t<td>"); 
      echo htmlentities($val, ENT_QUOTES); 
      echo("</td>\r\n"); 
     } 
    echo("</tr>\r\n"); 
    } 
    echo("</table>"); 
    fclose($handle); 
?> 

但我一直無法弄清楚如何抓住城市,國家數據和刪除重複項。有沒有人有建議?

+0

我期望當'$ index == 2'有城市時,以及'$ index == 3'你有國家。要刪除重複項,您需要參考數據數組或將信息存儲在更加用戶友好的數組中,以跟蹤已寫入的內容。 – bloodyKnuckles

回答

1

在CSV句柄循環中,首先用逗號連接城市和國家/地區字段,並檢查$ city_countries數組。如果沒有輸入城市國家字符串寫入$ city_countries數組。 TR標籤被回顯,並且$ data數組被循環寫出TD標籤和列值。

$handle = fopen("namelist.csv", "r"); 
$data = fgetcsv($handle, 1000, ","); 
$city_countries = array(); 
echo('<table>'); 
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    $city_country = $data[2] . ', ' . $data[3]; 
    if (!in_array($city_country, $city_countries)) { 
     array_push($city_countries, $city_country); 
    } 
    echo("<tr>\r\n"); 
    foreach ($data as $index=>$val) { 
     echo("\t<td>"); 
     echo htmlentities($val, ENT_QUOTES); 
     echo("</td>\r\n"); 
    } 
    echo("</tr>\r\n"); 
} 
echo("</table>"); 
fclose($handle); 

print '<pre>'; print_r($city_countries); print '</pre>'; 

這是我使用的輸入文件:

Last Name, First Name, City, Country, Language 
Smith, Joe, Shanghai, China, English 
Jackson, Stacey, Madrid, Spain, Spanish 
Jackson, Steve, Madrid, Spain, Spanish 
Jones, Bob, London, United Kingdom, English 
Seward, Elisa, Madrid, Spain, English 
Harrison, Tim, Berlin, Germany, German 
Jones, Bill, London, United Kingdom, English 
Jackson, Ralph, Madrid, Spain, Spanish 

這是我得到的輸出:我看上面的帖子

Smith Joe  Shanghai China   English 
Jackson Stacey Madrid Spain   Spanish 
Jackson Steve Madrid Spain   Spanish 
Jones Bob  London United Kingdom English 
Seward Elisa Madrid Spain   English 
Harrison Tim  Berlin Germany  German 
Jones Bill London United Kingdom English 
Jackson Ralph Madrid Spain   Spanish 

Array 
(
    [0] => Shanghai, China 
    [1] => Madrid, Spain 
    [2] => London, United Kingdom 
    [3] => Berlin, Germany 
) 
+0

感謝這個例子 - 它看起來像重複值正在打印,但。 – Marcatectura

+0

我看到了 - 代碼解析整行以找到重複項。我想單獨刪除城市,國家/地區列中的重複項,因此城市中應該只有一個「馬德里,西班牙」,而國家/地區陣列中應該只有一個全名/位置/語言表,不應篩選重複項 – Marcatectura

+0

。代碼調整以列出所有記錄,並存儲獨特的城市,國家組合。 – bloodyKnuckles

1

試試這個:(還沒有把目前訪問PHP,將尋找微小的蟲子,大約一個小時)

<?php 
    $handle = fopen("namelist.csv", "r"); 
    $data = fgetcsv($handle, 1000, ","); 
    $csv = array(); 
    $csv[] = array(); 
    $csv[] = array(); 
    $csv[] = array(); 
    $csv[] = array(); 
    $csv[] = array(); 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     foreach ($data as $index=>$val) { 
      $column=0; 
      $csv[column][] = htmlentities($val, ENT_QUOTES); 
      $column++; 
     } 
    } 
    fclose($handle); 
    //Now, csv[0] has all Last Names, csv[1] has all First Names, csv[2] all Cities, csv[3] all Countries and csv[4] all Languages 
    //To filter duplicates.. 
    $cities = array_unique($csv[2]); 
    $countries = array_unique($csv[3]); 
?> 

本(每列一個)創建一個擁有5個陣列陣列。那些數組然後填充CSV的每一行。之後,城市和國家的專欄被清除重複價值。如上所述,這段代碼應該可以工作,但是我還沒有能夠測試它,如果它沒有給我留言,我肯定會在今天下午晚些時候修復它。

1

$data[2]包含城市和申請array_unique(...)推後的所有城市值將數組將刪除重複。

$cities = array(); 

while (($data = fgetcsv($handle, 1000, ",")) !== false) { 
    $cities[] = $data[2]; 
} 

$cities = array_unique($cities); 

print_r($cities); 

參考php manual,它也有一堆樣本代碼。

1

當處理具有標題行的CSV時,我傾向於將數據列與命名鍵匹配,因此我不需要跟蹤特定列與哪個索引相關。這使您可以參考$var['ColumnName']而不是$var[2]

<?php 
$csvDelim = ','; 
$csvEnclosure = ''; 

$csvArr = file('./namelist.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 

//create array of the csv headers 
$csvHeaders = str_getcsv(trim(array_shift($csvArr)), $csvDelim, $csvEnclosure); 
$csvHeaders = array_map("trim", $csvHeaders); 

//get the csv data and make a multi-dim array of keys/values 
$dataArr = array(); 
foreach($csvArr as $csvLine) 
{ 
    $lineData = str_getcsv(trim($csvLine), $csvDelim, $csvEnclosure); 
    $lineData = array_map("trim", $lineData); 
    $dataArr[] = array_combine($csvHeaders, $lineData); 
} 

//get unique city/country values 
$locations = array(); 
foreach($dataArr as $da) 
    $locations[] = $da['City'].', '.$da['Country']; 

$locations = array_unique($locations); 

//output data in table 
echo '<table>'; 
echo '<tr>'; 
foreach($csvHeaders as $headerValue) 
    echo '<th>'.$headerValue.'</th>'; 
echo '</tr>'; 

foreach($dataArr as $dataLine) 
{ 
    echo '<tr>'; 
    foreach($dataLine as $dataValue) 
    { 
     echo '<td>'.htmlentities($dataValue, ENT_QUOTES).'</td>'; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
?> 
+0

感謝這個偉大的例子。我注意到,當我嘗試實現這個和'print_r($ locations)'時,我得到一個空數組:'Array([0] =>)' – Marcatectura

+0

@Marcatectura:對不起, 。我已經更新了答案。 – cOle2

2

這裏簡單的方式,也刪除重複的城市,而不必實際篩選他們。

$fHandle = fopen("namelist.csv", "r"); 
$aData = fgetcsv($handle, 1000, ","); 
while (($aData = fgetcsv($fHandle, 1000, ",")) !== FALSE) { 
    $aLocations[$aData[3]] = $aData[4]; 
} 

echo '<table>'; 
foreach ($aLocations as $sCity => $sCountry) { 
    echo '<tr><td>'.$sCity.'</td><td>'.$sCountry.'</td></tr>'; 
} 
echo '</table>'; 
0

它沒有正常工作 - 所以在這裏更新了James Hunt的工作代碼。

<?php 

$handle = fopen("test.csv", "r"); 
$data = fgetcsv($handle, 1000, ";"); 

$csv = array(); 
$csv[] = array(); 
$csv[] = array(); 
$csv[] = array(); 
$csv[] = array(); 
$csv[] = array(); 

while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { 

    $column=0; 

    foreach ($data as $index=>$val) { 

     $csv[$column][] = htmlentities($val, ENT_QUOTES); 
     $column++; 
    } 

} 

fclose($handle); 
//Now, csv[0] has all Last Names, csv[1] has all First Names, csv[2] all Cities, csv[3] all Countries and csv[4] all Languages 
//To filter duplicates.. 
$cities = array_unique($csv[2]); 
$countries = array_unique($csv[3]); 

var_dump($cities); //will output all column values of $csv[2] 

?> 

敬請期待 - greetz Robert!