Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查csv文件中的值在哪一行更改?_Php_Html_Csv_While Loop - Fatal编程技术网

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

Php 如何检查csv文件中的值在哪一行更改?,php,html,csv,while-loop,Php,Html,Csv,While Loop,我正在创建一个表,其中包含一家公司的电话号码,该公司在不同的组织和其他部门中是分开的。我将这些信息保存在CSV文件中 我的问题是,我如何检查部门何时发生变化,以便我可以创建一个间距,或者我可以在以后设置样式 This is the Structure of my CSV file. organization;name;phonenumber;department PHP脚本: function printPhonenumbers($org) { $handle = fopen ('csv

我正在创建一个表,其中包含一家公司的电话号码,该公司在不同的组织和其他部门中是分开的。我将这些信息保存在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);
}
函数printPhoneNumber($org){ $handle=fopen('csvPHP.csv','r'); while($csv_array=fgetcsv($handle,0,,;'))!==FALSE){
对于@Matit指出的($i=5;$i),试着记住最后一个已知值:

例如:

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);
}
函数printPhoneNumber($org){ $handle=fopen('csvPHP.csv','r'); $last_department=null; while($csv_array=fgetcsv($handle,0,,;'))!==FALSE){
对于($i=5;$i),您需要以某种方式记住(保存?)以前的值,以便知道新值是否不同。@matit您是否有任何简单的示例,我已经尝试过多次,但我不知道如何以智能方式“保存”它