【问题标题】:Returning on a recursive function in php返回php中的递归函数
【发布时间】:2011-05-23 16:10:00
【问题描述】:

我正在开发一个函数来解析 2 个 xml 文件。它逐个节点比较它们,然后如果节点不同,则该函数应返回其中之一。但它没有返回任何东西。

$xml = simplexml_load_file("file1.xml");
$xml2 = simplexml_load_file("file2.xml");

$result = parseNode($xml, $xml2);
print_r($result);
echo $result;

function parseNode($node1, $node2) {

    for ($i = 0; $i < count($node1->children()); $i++) {

        $child1 = $node1->children();
        $child2 = $node2->children();

        if ($child1[$i]->getName() != $child2[$i]->getName()) {
            return $child1[$i];
        } else {
            parseNode($child1[$i], $child2[$i]);
        }

    }
}

【问题讨论】:

    标签: php xml parsing recursion return


    【解决方案1】:

    您还可以使用递归迭代器循环遍历 XML 结构以简化您的 parseNodes() 函数。

    $xml  = simplexml_load_string("<root><foo/><bar><baz/></bar></root>", "SimpleXMLIterator");
    $xml2 = simplexml_load_string("<root><foo/><bar><baz/></bar><bat/></root>", "SimpleXMLIterator");
    
    $result = parseNode($xml, $xml2);
    echo $result;
    
    function parseNode($a, $b) {
        $mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY|MultipleIterator::MIT_KEYS_NUMERIC);
        $mit->attachIterator(new RecursiveIteratorIterator($a, RecursiveIteratorIterator::SELF_FIRST));
        $mit->attachIterator(new RecursiveIteratorIterator($b, RecursiveIteratorIterator::SELF_FIRST));
    
        foreach ($mit as $node) {
            // One has more nodes than another!
            if (  ! isset($node[0], $node[1])) {
                return 'Curse your sudden but inevitable betrayal!';
            }
            // Nodes have different names
            if ($node[0]->getName() !== $node[1]->getName()) {
                return $node[0];
            }
        }
    
        // No differences in names and order
        return FALSE;
    }
    

    设置MultipleIterator 非常冗长(主要是由于类名超长),但逻辑非常简单。

    【讨论】:

      【解决方案2】:

      嗯,你可以用一个简单的条件语句来做到这一点......

      $xml = simplexml_load_file("file1.xml");
      $xml2 = simplexml_load_file("file2.xml");
      
      $result = parseNode($xml, $xml2);
      print_r($result);
      echo $result;
      
      function parseNode($node1, $node2) {
          $child1 = $node1->children();
          $child2 = $node2->children();
          $numChildren = count($child1);
          for ($i = 0; $i < $numChildren; $i++) {
              if ($child1[$i]->getName() != $child2[$i]->getName()) {
                  return $child1[$i];
              } else {
                  $test = parseNode($child1[$i], $child2[$i]);
                  if ($test) return $test;
              }
          }
          return false;
      }
      

      【讨论】:

        【解决方案3】:

        递归调用中没有return。因此,没有结果。

        编辑不要对此投票。 ircmaxell 是对的。我已经删除了答案的异常部分。

        【讨论】:

        • -1 表示滥用异常。这不是一个例外情况,所以没有理由与例外相关的所有开销......
        • 那会是什么开销?哪里有“虐待”?不是流控语句吗?
        • print_r($result) 和 echo $result 仍然不打印任何内容。
        • 生成的完整回溯。例外并不便宜。并且不应在非特殊情况下滥用它们。我强烈建议阅读Code Complete 2...
        • 确实如此。但我需要该函数来返回其中一个节点。
        【解决方案4】:
               return parseNode($child1[$i], $child2[$i]);
        

        【讨论】:

        • print_r($result) 和 echo $result 仍然不打印任何内容。
        • 我遇到了相关问题。递归不会返回比根级别更深的数组键上的任何内容。添加了回报并且它起作用了。谢谢@hwlau
        猜你喜欢
        • 2013-09-21
        • 2012-03-11
        • 2015-04-27
        • 2018-02-21
        • 2011-04-20
        • 2018-01-04
        • 2021-03-02
        • 2021-08-14
        • 1970-01-01
        相关资源
        最近更新 更多