返回php中的递归函数

发布于 2024-10-07 09:14:32 字数 623 浏览 0 评论 0原文

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

    }
}

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");

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽袖吟 2024-10-14 09:14:32
       return parseNode($child1[$i], $child2[$i]);
       return parseNode($child1[$i], $child2[$i]);
み零 2024-10-14 09:14:32

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

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

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;
}
提笔落墨 2024-10-14 09:14:32

您还可以使用递归迭代器循环遍历 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 相当冗长(主要是由于类名超长),但逻辑非常简单。

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.

爱的故事 2024-10-14 09:14:32

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

编辑不要对此进行投票。 ircmaxell是对的。我已经删除了答案中的例外部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文