从PHP数组中获取特定的CSV列或第n个值

时间:2014-06-24 15:33:54

标签: php arrays csv

我正在使用PHP打开并解析一个非常小的(大约1kb)CSV文件来生成HTML表格。我是PHP新手,这在很大程度上是实验性的。除了生成HTML表格之外,我正在尝试从该csv(城市和国家/地区信息)中的特定列集生成数组,然后删除重复值。 CSV的结构如下:

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

这里的想法是,除了包含所有数据的表格之外,我还会列出表格中列出的所有城市/国家/地区:

  • 中国上海
  • 西班牙马德里
  • 英国伦敦
  • 德国柏林

感谢关于Stack Overflow的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);
?>

但我一直无法弄清楚如何获取城市,国家数据和删除重复项。有没有人有建议?

6 个答案:

答案 0 :(得分:3)

这是一种简单的方法,可以删除重复的城市,而无需对其进行过滤。

$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>';

答案 1 :(得分:1)

试试这个:( Haven目前无法访问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的每一行填充这些数组。之后,城市和国家/地区列将清除重复值。如上所述,此代码应该可以工作,但我还没有能够测试它,如果它没有给我留言,我肯定会在今天下午晚些时候修复它。

答案 2 :(得分: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,其中包含一堆示例代码。

答案 3 :(得分: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>';
?>

答案 4 :(得分:1)

在CSV句柄循环中,首先,使用逗号连接city和country字段,并检查$ city_countries数组。如果没有dupes,则将city country字符串写入$ city_countries数组。 TR标签是echo,并且$ 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
)

答案 5 :(得分: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!