Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.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/arrays/13.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 抓取并分解数据_Php_Arrays_Foreach_Explode - Fatal编程技术网

Php 抓取并分解数据

Php 抓取并分解数据,php,arrays,foreach,explode,Php,Arrays,Foreach,Explode,我对PHP有点陌生,我需要一些关于从文件中分解数据的帮助。有关文件是: 基本上,我需要在下获取数据!客户端:部分(靠近底部)。有了这些数据,我需要将其分解,并获得每个:之间的信息 我尝试过使用这段代码,但它给了我一个可变偏移量错误(未定义的偏移量:3) 如果有人能帮我解决这个问题,我将不胜感激。发生这种情况是因为您试图像这样分解行: !“常规”包含常规设置 分解该行时,您的$data\u记录如下所示: 排列( [0]=>;!常规包含常规设置) 快速解决方案: $file = file("htt

我对PHP有点陌生,我需要一些关于从文件中分解数据的帮助。有关文件是:

基本上,我需要在
下获取数据!客户端:
部分(靠近底部)。有了这些数据,我需要将其分解,并获得每个
之间的信息

我尝试过使用这段代码,但它给了我一个可变偏移量错误(
未定义的偏移量:3


如果有人能帮我解决这个问题,我将不胜感激。

发生这种情况是因为您试图像这样分解行:

!“常规”包含常规设置

分解该行时,您的
$data\u记录如下所示:

排列( [0]=>;!常规包含常规设置)

快速解决方案:

$file = file("http://data.vattastic.com/vatsim-data.txt");
foreach($file as $line)
{
   if(strpos($line,';') === 0) continue ; // this is comment. ignoring
   $data_record = explode(":", $line);
   $col_count = count($data_record);

   switch($col_count) {
     case 42: // columns qty = 42, so this is row from `clients`
      // grab only the data that has "ATC" in it...
      if($data_record[3] == 'ATC' && $data_record[16] != '1' && $data_record[18] != '0'&&   stristr($data_record[0],'OBS') === FALSE)
      {
           rest of code here...
      }
      break;
     default:
       // this is other kind of data, ignoring
       break;
   }
}

另一个解决方案是使用正则表达式并查找
!客户:
部分。这也适用于将来客户机的列数大于或小于42列的情况

$file = file_get_contents ("http://data.vattastic.com/vatsim-data.txt");  
$matches = null;
preg_match ('/!CLIENTS:\s\n(.*)\n;/s' , $file, $matches );
if($matches)
{
  $client_lines = explode("\n", $matches[1]);
  foreach ($client_lines as $client)
  {
    $data_record = explode(":", $client);
    if($data_record[3] == 'ATC' && $data_record[16] != '1' && $data_record[18] != '0'&&   stristr($data_record[0],'OBS') === FALSE)
    {
      //rest of code here...
    }
  }
}

使用
var\u dump($data\u record)
。问题是
$data\u记录
没有第三个位置
var\u dump($data\u记录)
正在返回该数据文件中的所有内容。把东西放在
下面最好的方法是什么!客户端:
section?看起来像是
explode(“:”,$line)没有返回您期望的内容。在尝试访问之前,您应该测试
$data\u record
的元素数是否正确执行
$data\u record=explode(“!CLIENTS”,$line)
并使用
$data\u record[1]
;)这个文件多久更改一次?我的正则表达式技能不是很好,但尝试过,效果很好,但可能有更好的正则表达式来解决这个问题。我会记住你的代码,以防列数发生变化。谢谢这似乎很有效。我会再测试一下,让你知道。一切似乎都正常。谢谢你的帮助!
$file = file_get_contents ("http://data.vattastic.com/vatsim-data.txt");  
$matches = null;
preg_match ('/!CLIENTS:\s\n(.*)\n;/s' , $file, $matches );
if($matches)
{
  $client_lines = explode("\n", $matches[1]);
  foreach ($client_lines as $client)
  {
    $data_record = explode(":", $client);
    if($data_record[3] == 'ATC' && $data_record[16] != '1' && $data_record[18] != '0'&&   stristr($data_record[0],'OBS') === FALSE)
    {
      //rest of code here...
    }
  }
}