Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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_Xml_Parsing_Recursion_Return - Fatal编程技术网

返回php中的递归函数

返回php中的递归函数,php,xml,parsing,recursion,return,Php,Xml,Parsing,Recursion,Return,我正在开发一个解析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) {

我正在开发一个解析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]);
        }

    }
}
$xml=simplexml\u load\u文件(“file1.xml”);
$xml2=simplexml\u加载文件(“file2.xml”);
$result=parseNode($xml,$xml2);
打印(结果);
回声$结果;
函数parseNode($node1,$node2){
对于($i=0;$ichildren());$i++){
$child1=$node1->children();
$child2=$node2->children();
如果($child1[$i]->getName()!=$child2[$i]->getName()){
返回$child1[$i];
}否则{
parseNode($child1[$i],$child2[$i]);
}
}
}

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


编辑不要向上投票。马克塞尔是对的。我已经删除了答案中的异常部分。

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

       return parseNode($child1[$i], $child2[$i]);

编辑不要向上投票。马克塞尔是对的。我已经删除了我答案中的例外部分。

好吧,你可以用一个简单的条件语句来完成

       return parseNode($child1[$i], $child2[$i]);
$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;
}
$xml=simplexml\u load\u文件(“file1.xml”);
$xml2=simplexml\u加载文件(“file2.xml”);
$result=parseNode($xml,$xml2);
打印(结果);
回声$结果;
函数parseNode($node1,$node2){
$child1=$node1->children();
$child2=$node2->children();
$numChildren=计数($child1);
对于($i=0;$i<$numChildren;$i++){
如果($child1[$i]->getName()!=$child2[$i]->getName()){
返回$child1[$i];
}否则{
$test=parseNode($child1[$i],$child2[$i]);
if($test)返回$test;
}
}
返回false;
}

好吧,你可以用一个简单的条件语句来实现

$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;
}
$xml=simplexml\u load\u文件(“file1.xml”);
$xml2=simplexml\u加载文件(“file2.xml”);
$result=parseNode($xml,$xml2);
打印(结果);
回声$结果;
函数parseNode($node1,$node2){
$child1=$node1->children();
$child2=$node2->children();
$numChildren=计数($child1);
对于($i=0;$i<$numChildren;$i++){
如果($child1[$i]->getName()!=$child2[$i]->getName()){
返回$child1[$i];
}否则{
$test=parseNode($child1[$i],$child2[$i]);
if($test)返回$test;
}
}
返回false;
}

您还可以使用递归迭代器在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;
}
$xml=simplexml\u load\u字符串(“,“SimpleXMLIterator”);
$xml2=simplexml_load_字符串(“,“SimpleXMLIterator”);
$result=parseNode($xml,$xml2);
回声$结果;
函数parseNode($a,$b){
$mit=新的乘法器(乘法器::mit_NEED_ANY |乘法器::mit_KEYS_NUMERIC);
$mit->attachIterator(新的递归迭代器($a,递归迭代器::SELF_FIRST));
$mit->attachIterator(新的递归迭代器($b,递归迭代器::SELF_FIRST));
foreach($mit作为$node){
//一个节点比另一个节点多!
如果(!isset($node[0],$node[1])){
return“诅咒你突然但不可避免的背叛!”;
}
//节点有不同的名称
如果($node[0]->getName()!==$node[1]->getName()){
返回$node[0];
}
}
//名称和顺序没有区别
返回FALSE;
}

设置
乘法器
非常冗长(主要是因为类名很长),但逻辑非常简单。

您还可以使用递归迭代器循环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;
}
$xml=simplexml\u load\u字符串(“,“SimpleXMLIterator”);
$xml2=simplexml_load_字符串(“,“SimpleXMLIterator”);
$result=parseNode($xml,$xml2);
回声$结果;
函数parseNode($a,$b){
$mit=新的乘法器(乘法器::mit_NEED_ANY |乘法器::mit_KEYS_NUMERIC);
$mit->attachIterator(新的递归迭代器($a,递归迭代器::SELF_FIRST));
$mit->attachIterator(新的递归迭代器($b,递归迭代器::SELF_FIRST));
foreach($mit作为$node){
//一个节点比另一个节点多!
如果(!isset($node[0],$node[1])){
return“诅咒你突然但不可避免的背叛!”;
}
//节点有不同的名称
如果($node[0]->getName()!==$node[1]->getName()){
返回$node[0];
}
}
//名称和顺序没有区别
返回FALSE;
}

设置
乘法器
非常冗长(主要是因为类名很长),但逻辑非常简单。

-1用于滥用异常。这不是一个例外情况,因此没有理由产生与异常相关的所有开销…这将是什么开销?哪里有“虐待”?这不是流控制语句吗?print\r($result)和echo$result仍然不打印任何内容。生成的完整回溯。例外情况并不便宜。不应在非特殊情况下滥用这些权利。我强烈建议大家读一读……事实上确实如此。但是我需要函数返回一个节点。-1用于滥用异常。这不是一个例外情况,因此没有理由产生与异常相关的所有开销…这将是什么开销?哪里有“虐待”?这不是流控制语句吗?print\r($result)和echo$result仍然不打印任何内容。生成的完整回溯。例外情况并不便宜。不应在非特殊情况下滥用这些权利。我强烈建议大家读一读……事实上确实如此。但是我需要函数返回一个节点。print\r($result)和echo$result仍然不打印任何内容。我遇到了一个相关问题。递归不会返回任何已被反求的数组键