简体   繁体   English

在较大的数组树中搜索数组键分支 - PHP

[英]Searching for an array key branch inside a larger array tree - PHP

The problem is similar to this Find an array inside another larger array问题类似于此Find an array inside another large array

The differences are that instead of searching for values, I'm searching for a small array key branch inside a larger array key tree.不同之处在于,我不是在搜索值,而是在较大的数组键树中搜索一个小的数组键分支。

Basically here's the array key branch that I'm looking for:基本上这里是我正在寻找的数组键分支:

$mission_parameters['stmt_echo']['subnodes']['exprs'][0]['expr_constfetch']['subnodes']['name']['name']['subnodes']['parts'][0] = 'true';

The array key tree is a very large tree which means it is multidimensional, and it may include $mission_parameters at any point in the tree.数组键树是一棵非常大的树,这意味着它是多维的,并且它可能在树的任何点包含 $mission_parameters。

So it's kind of like trying to find a yellow tree branch in a brown tree which may or may not have a yellow tree branch.所以这有点像试图在可能有也可能没有黄色树枝的棕色树中找到黄色的树枝。

Comparing the value at the end of the branch is also necessary.比较分支末尾的值也是必要的。

I'm looking at array_intersect, but it doesn't work on multidimensions.我正在查看 array_intersect,但它不适用于多维。 Has anyone solved this kind of problem before?以前有人解决过这种问题吗?

Note this is not the same as searching array within an array.请注意,这与在数组中搜索数组不同。 I'm not searching values.我不是在搜索值。

--EDIT-- - 编辑 -

Here's an example:下面是一个例子:

I'm looking for我在找

array(
    'statement' => array(
        'statement2' => array(
            0 => 'true',
        ),
    ),
);

Inside a larger array like this:在这样一个更大的数组中:

array(
    'statement4' => array(
        'statement' => array(
            'statement2' => array(
                0 => 'true',
            ),
            'statement3' => array(
                2 => 'false',
            ),
        ),
    ),
);

Do you see how the smaller array is like a branch to the larger array.您是否看到较小的数组就像是较大数组的分支。 The larger array currently contains the smaller branch, but it has other sorts of other elements.较大的数组当前包含较小的分支,但它还有其他种类的其他元素。 Therefore I'm searching an array key branch.因此我正在搜索一个数组键分支。 In this example it would be [statement][statement2][0] = 'true'在这个例子中,它是[statement][statement2][0] = 'true'

I think (and you have confirmed) that you can represent your data as an XML structure.我认为(并且您已经确认)您可以将数据表示为 XML 结构。 I this case, you can use XPath to find any branch in your data.在这种情况下,您可以使用XPath查找数据中的任何分支。 Here is sample XML data inferred from your question:以下是从您的问题中推断出的示例 XML 数据:

<?xml version="1.0" encoding="utf-8" ?>
<stmt_echo>
    <subnodes>
        <exprs>
            <expr_constfetch>
                <subnodes>
                    <name>
                        <name>
                            <subnodes>
                                <parts>
                                    <part>true</part>
                                    <!-- more items -->
                                </parts>
                            </subnodes>
                        </name>
                    </name>
                </subnodes>
            </expr_constfetch>
            <!-- more items -->
        </exprs>
    </subnodes>
</stmt_echo>

And here is the PHP code to locate the node you mentioned:这是用于定位您提到的节点的 PHP 代码:

$doc = new DOMDocument;
$doc->Load("test.xml");

$xpath = new DOMXPath($doc);
$nodes = $xpath->query("//subnodes/parts[part='true']");

foreach($nodes as $node) {
    dumpNodePath($node);
}

# Output
# /stmt_echo/subnodes/exprs/expr_constfetch/subnodes/name/name/subnodes/parts

function dumpNodePath($node) {
    $path = array($node->tagName);
    while ($node = $node->parentNode) {
        if ($node->nodeType != XML_DOCUMENT_NODE) {
            $path[] = $node->tagName;
        }
    }
    echo "/" . implode("/", array_reverse($path)) . "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM