dstjh46606 2012-12-03 22:21
浏览 26
已采纳

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

The problem is similar to this Find an array inside another larger 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.

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. 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'

  • 写回答

1条回答 默认 最新

  • drydaenth257216154 2012-12-04 15:54
    关注

    I think (and you have confirmed) that you can represent your data as an XML structure. I this case, you can use XPath to find any branch in your data. Here is sample XML data inferred from your question:

    <?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:

    $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)) . "
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行