开发者

Returning on a recursive function in php

开发者 https://www.devze.com 2023-01-29 18:34 出处:网络
I\'m developing a function to parse 2 xml files. It compares them node by node and then if the nodes are different, the function should return one of them. But it isn\'t returning anything.

I'm developing a function to parse 2 xml files. It compares them node by node and then if the nodes are different, the function should return one of them. But it isn't returning anything.

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

$resu开发者_StackOverflow中文版lt = 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]);
        }

    }
}


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


Well, you can do it with a simple conditional statement...

$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;
}


You could also loop over the XML structures using recursive iterators to simplify your parseNodes() function.

$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;
}

Setting up the MultipleIterator is pretty verbose (mostly due to the über-long class names) but the logic is darned simple.


There's no return in the recursive call. Hence, no result.

EDIT Do not up-vote this. ircmaxell is right. I have removed the exception part of my answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消