我如何检查csv文件中值的更改在哪一行?

问题描述 投票:-1回答:1

我正在创建一个表格,其中包含一家公司的电话号码,这些公司分别位于不同的组织和其他部门。我已将这些信息保存在CSV文件中。

我的问题是,如何检查部门变更的时间,以便可以进行间隔或以后进行样式设置?

This is the Structure of my CSV file.

organization;name;phonenumber;department

PHP脚本:

function printPhonenumbers($org) {
  $handle = fopen ('csvPHP.csv','r');
  while (($csv_array = fgetcsv ($handle, 0, ';')) !== FALSE ) {
    for ($i=5; $i <= count($csv_array) ; $i=$i+1) {
      if ($csv_array[0] == $org) {
        echo "<tr>";
        echo "<td>".$csv_array[1]."</td>";
        echo "<td>".$csv_array[2]."</td>";
        echo "</tr>";
      }
    }
  }
  fclose($handle);
}
php html csv while-loop
1个回答
0
投票

正如@matiit指出的那样,请记住最后一个已知值:

示例:

function printPhonenumbers($org) {
  $handle = fopen ('csvPHP.csv','r');
  $last_department = null;
  while (($csv_array = fgetcsv ($handle, 0, ';')) !== FALSE ) {
    for ($i=5; $i <= count($csv_array) ; $i=$i+1) {
      if ($last_department != $csv_array[3]) {
         // here the department is different from the previous one. Remember to 'save' the new value
         $last_department = $csv_array[3];
         // do your thing
      } else {
         // still the same department
         if ($csv_array[0] == $org) {
           echo "<tr>";
           echo "<td>".$csv_array[1]."</td>";
           echo "<td>".$csv_array[2]."</td>";
           echo "</tr>";
         }
      }
    }
  }
  fclose($handle);
}
© www.soinside.com 2019 - 2024. All rights reserved.