【问题标题】:Filtering CSV using PHP使用 PHP 过滤 CSV
【发布时间】:2014-09-01 23:24:20
【问题描述】:

您好,我有一个 CSV 文件需要过滤,从 A-C、D-F 等

    Aaron, Male, aaron@website.com
    Arianne, Female, arianne@something.com
    Bea, Female, bea@hello.com
    Carlos, Male, carlos@website.com
    Drake, Male, drake@website.com
    Delilah, Female, del@hello.com
    Erica, Female, erika@webisite.com
    Flint, Male, flint@something.com

我不擅长 PHP,我尝试通过执行以下代码来修复它。

<?php
$file  = fopen('LoggedInUsers.csv', 'r');

$first = array('Aaron');
$last = array('Carlos');



while (($line = fgetcsv($file)) !== FALSE) {  
    list($name, $log, $department) = $line;

    $firstt = array('Aaron');
    if ($first < 0) {
    }
    $last = array ('Carlos');
    if ($last < 0) {
}
?>

【问题讨论】:

  • 您要过滤或修复什么?这可能是关于排序吗?你打算$first$last 做什么?

标签: php csv filtering


【解决方案1】:

您可以使用substr 从字符串中获取特定字符,但有一种特定方法可以比较名为strncasecmp 的字符串的前n 字符

首先,你能保证CSV中的记录是按字母顺序排序的吗?我假设不是,所以我们将遍历每一行并将匹配的行添加到数组中;

$first = "A";
$last = "C";

$Ret = array();

while (($line = fgetcsv($file)) !== FALSE) {  
    list($name, $log, $department) = $line;

    if (strncasecmp($name, $first, 1) >= 0 && strncasecmp($name, $last, 1) <= 0) {
        array_push($Ret, array($name, $log, $department));
    }
}

//Show what we got
print_r($Ret);

运行此命令后,$Ret 应包含所有以从 $first$last(包括)的字母开头的名称

这有一个额外的好处,你不需要知道具体的名字,只需要知道你感兴趣的字母。

请注意,strncasecmp 用于不区分大小写的字符串比较。基本上,如果字符串的第一个 n 字符相等,则返回 0,如果第一个字符串在第二个字符串“之前”,则返回负值,如果在“之后”,则返回正值。

【讨论】:

    【解决方案2】:

    我要做的是首先创建一个伪分组结构(一个数组)A-C, D-F, ...,用于分组,然后检查迭代中当前的第一个字母,然后根据其所属格式将其推入内部。示例:

    $filters = array_chunk(range('a', 'z'), 3); // grouping structure
    $file  = fopen('LoggedInUsers.csv', 'r');
    
    $data = array();
    while (($line = fgetcsv($file)) !== false) {
        list($name, $log, $department) = $line;
        foreach($filters as $batch =>$filter_batch) {
            // if the current name does belong to this structure, push it inside
            if(in_array(strtolower(substr($name, 0, 1)), $filter_batch)) {
                $data[$batch][] = $name;
            }
        }
    }
    
    echo '<pre>';
    print_r($data);
    

    所以输出应该是:

    Array
    (
        [0] => Array // batch of A-C
        (
            [0] => Aaron
            [1] => Arianne
            [2] => Bea
            [3] => Carlos
        )
    
        [1] => Array // batch of D-F
        (
            [0] => Drake
            [1] => Delilah
            [2] => Erica
            [3] => Flint
        )
    
    ) 
    

    【讨论】:

      【解决方案3】:
      /**
       * Function to sort multidimensional array on first element using 
       * PHP's usort().
       * @see http://uk1.php.net/manual/en/function.usort.php
       **/
      function username_sort( $foo, $bar ){
          return ( $foo[0] > $bar[0] );
      }
      
      // Gets all file content into a multidimensional array in memory
      $loggedInUsers = array_map( 'str_getcsv', file( 'LoggedInUsers.csv' ) );
      
      // Sorts the array by the first element (username) in a copy of the data.
      $sorted_loggedInUsers = usort( $loggedInUsers, 'username_sort' );
      

      此时,您将数据放在一个排序数组中,可以是array_slice()d,但您希望这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-04
        • 2011-01-23
        • 2017-11-10
        • 2022-01-09
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 2014-10-01
        相关资源
        最近更新 更多