【问题标题】:Extract cell value from table based on another cells value根据另一个单元格值从表中提取单元格值
【发布时间】:2013-01-10 22:41:48
【问题描述】:

HTML 文件: http://www.arifoorum.com/test/html.htm

我通过 simplehtmldom 库得到了这个 html 内容:

array(66) {
  [0]=>
  array(14) {
    [0]=>
    string(4) "Item"
    [1]=>
    string(11) "Date, time:"
    [2]=>
    string(8) "mõõdikud"
    [3]=>
    string(6) "Name 2"
    [4]=>
    string(6) "Name 3"
    [5]=>
    string(9) "Meter ID:"
    [6]=>
    string(6) "V_HeEn"
    [7]=>
    string(6) "U_HeEn"
    [8]=>
    string(3) "V_V"
    [9]=>
    string(3) "U_V"
    [10]=>
    string(6) "V_InTe"
    [11]=>
    string(6) "U_InTe"
    [12]=>
    string(6) "V_OuTe"
    [13]=>
    string(6) "U_OuTe"
  }
  [1]=>
  array(14) {
    [0]=>
    string(1) "1"
    [1]=>
    string(19) "24.01.2013 22:23:33"
    [2]=>
    string(9) "Meter 002"
    [3]=>
    string(6) " "
    [4]=>
    string(6) " "
    [5]=>
    string(8) "40380040"
    [6]=>
    string(6) " "
    [7]=>
    string(6) " "
    [8]=>
    string(6) " "
    [9]=>
    string(6) " "
    [10]=>
    string(6) " "
    [11]=>
    string(6) " "
    [12]=>
    string(6) " "
    [13]=>
    string(6) " "
  }
  [2]=>
  ...
  }
}

完整输出:http://www.arifoorum.com/test/test.php

如何从该数组中获取特定元素?

例如:假设我想要 mõõdikud = 01 和 name 2 = külm 的值(应该是 72,114)。

谢谢

【问题讨论】:

  • 我很抱歉,我对数组很陌生。单词迭代和比较没有例子没有帮助:(
  • PHP 手册有一个nice page about arrays。如果您对数组还不是很熟悉,我真的建议您阅读它。循环是一种语言结构,只要满足条件,就可以重复一组命令,并且可以使用它们来迭代数组的每个元素。看看for 和foreach loop。

标签: php html arrays dom


【解决方案1】:

这可能对其他用户有用,所以我做了一个小函数,根据其他单元格的值(条件)从表格中获取单元格的值:

function getCellValue(DOMElement $table, $cellName = null, array $conditions = array()){

  // get all table rows
  $trs = $table->getElementsByTagName('tr');  

  // assume first TR is the table header
  $head = $trs->item(0);

  // find cell names and their index
  $keys = array();
  foreach($head->childNodes as $th)
    if(!($th instanceof DomText))
      $keys[] = trim($th->nodeValue);

  if($invalidKeys = array_diff(array_keys($conditions), $keys))
    throw new Exception(sprintf('Non-extistent key(s) in table: ', implode(', ', $invalidKeys)));

  // find the row that meets all conditions
  $targetRow = null;
  foreach($table->childNodes as $tr){

    // internal counter because we can't rely on DOM index
    $idx = 0;
    foreach($tr->childNodes as $td){

      if($td instanceof DomText)
        continue;

      $value = trim($td->nodeValue);

      // check if all conditions match
      if(array_key_exists($keys[$idx], $conditions))
        $targetRow = ($value != $conditions[$keys[$idx]]) ? null : $tr;

      $idx++;    
    }

    // stop if we found a match
    if($targetRow)
      break;
  }

  if(!$targetRow)
    throw new Exception('No row matches your conditions');

  // build an array with row cells
  $values = array();
  $idx = 0;
  foreach($targetRow->childNodes as $td)
    if(!($td instanceof DomText))
      $values[$keys[$idx++]] = trim($td->nodeValue);

  // return the cell value if a specific cell was requested
  if($cellName !== null)
    return isset($values[$cellName]) ? $values[$cellName] : null;

  // otherwise return all values from the matched row
  return $values;
}

它使用DomDocument,因为该问题未标记为

@OP:在你的情况下,你会像这样使用它:

$html = file_get_contents('http://www.arifoorum.com/test/html.htm');

$dom = new DomDocument();
$doc->preserveWhiteSpace = false;
$dom->loadHtml($html);

$table = $dom->getElementsByTagName('table')->item(0);

print getCellValue($table, 'V_V', array(
  'mõõdikud' => '01',
  'Name 2'   => 'külm',
));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多