-2

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'

Community
  • 1
  • 1
CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98
  • 2
    It's not clear what you're trying to do, and your description makes no sense. You're giving an example of what you're looking for, but then neglect to explain how it is not actually the answer to your question. Providing some actual example data (not just the array element expression you provided) and expected result might help. – lanzz Dec 03 '12 at 22:28
  • It has been edited for your pleasure. – CMCDragonkai Dec 04 '12 at 14:59

1 Answers1

1

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)) . "\n";
}
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • I was thinking about this when I was on the train. In order to construct that query, I would need to use eval. One more problem would be that I could not check multi branches. Like if my search array wasn't just a direct one way branch, but if it had 2 end points. – CMCDragonkai Dec 04 '12 at 16:03
  • Oh dear, are you saying that `statement->statement2->0` is just an example and you will, for example, search for expressions? – Salman A Dec 04 '12 at 16:14
  • It is just an example. For now I'm only searching single line branches with one endpoints, and I was thinking of just manually adding more single lines to test. But that isn't very efficient. Not sure what you mean by expressions? The large array structure is basically like the sample I gave, but it's much larger and has many child nodes and multiple endpoints. It's actually an abstract syntax tree from a static code analysis on a PHP script. – CMCDragonkai Dec 04 '12 at 16:29
  • I have a suggestion, see if it makes sense. Instead of building a nested associative array structure, build an XML structure. This should be straight forward. Then use [XPath queries](http://php.net/manual/en/domxpath.query.php) to find the nodes. If you have not used XPath before, you will be surprised at the ways it lets you search the tree. – Salman A Dec 04 '12 at 16:50
  • You're right. I can export the data as XML. Let me try that now. – CMCDragonkai Dec 05 '12 at 11:33
  • Hey I've got actually to this and further. Right now I'm trying to evaluate the path. But I have a problem with namespaces. The XML document I have has namespaces, but I don't know how to query them. Which I asked here: http://stackoverflow.com/questions/13730539/what-is-a-multi-branch-xpath-query-for-finding-this – CMCDragonkai Dec 05 '12 at 19:20