tags:

views:

59

answers:

3

I want to transform the 'values' array created by xml_parse_into_struct() into a bunch of nested arrays which I can walk recursively. This is for a very simple XML class which will hierarchically search the document like so:

$xml_data = "
<sometag>
    <someothertag>
        <somedata>foo</somedata>
    </someothertag>
    <someothertag>
        <somedata>bar</somedata>
    </someothertag>
</sometag>
<sometag>
    <someothertag>
        <somedata>baz</somedata>
    </someothertag>
</sometag>";

$parser = new Xml_Data($xml_data);
$somedata = $parser->find('sometag')->find('someothertag')->results();
// 0: "somedata"
//    "value": "foo"
// 1: "somedata"
//    "value": "bar"

// etc.

storing it in nested associative arrays would make it much easier to work with than keeping track of each opening and closing tag and what 'level' they occur at like xml_parse_into_struct outputs. But I wonder -- if the document gets pretty big, will this huge array be horrible? Should I just give up and traverse the stupid version of the array that PHP gives me?

A: 

Not sure about your exact requirement but I think you can use array_walk_recursive there.

Sarfraz
yes, but then I have to transform the array from `xml_parse_into_struct` -- recursive is my preference, but I don't know if doing it that way will result in cache misses and stack overflows
Carson Myers
then you should use SimpleXML i suppose.
Sarfraz
+3  A: 

Why not to use SimpleXML for that? The same nesting model, but objects instead of arrays.

FractalizeR
I'm so sad I didn't find this earlier
Carson Myers
Shouldn't you be glad as well that now you found it? :)
o.k.w
well, yes, but sad about time wasted haha.
Carson Myers
;) Well, I am sad about my reply is not marked as a valid one with green mark ;) :) :) But anyway, the point is that the problem is solved and this is the only important thing ;) Good luck.
FractalizeR
Using objects would create a fair bit of overhead that is avoided using my nested array solution. Just to defend myself a little. ;)
Tor Valamo
I accepted Tor's answer because, although this answer solves my _problem_ better, Tor did a better job of answering my question, which was about the overhead of (potentially) heavily nested arrays
Carson Myers
You sure? :) How much memory/time will xml_parse_into_struct take for itself and how much memory / time will be eaten during conversion into the form you suggested? :) Of course by itself, arrays with only information necessary are more compact, than SimpleXML objects, but... You need to build them first.
FractalizeR
My solution builds just as fast as yours. The minimum for a markup parser is O(n), since it has to go through everything at least once. You can easily build my array using start-tag/data/end-tag callbacks, which results in worst case O(n).
Tor Valamo
Don't get me wrong, I went with SimpleXML, but if someone stumbles across this question wondering about nested arrays but with nothing to do with XML, they will want to read an answer about nested arrays
Carson Myers
+2  A: 

For best effect you'd might wanna do

$xml['sometag'][0]['someothertag'][1]['somedata'][0] # bar

Where the array has two dimensions for each level. That is, pairs of tagname/tagindex values.

This is both scalable and readable.

Tor Valamo
awesome, this is better than how I first thought of doing it. It turns out a library already exists for this, which I didn't know. But this answers my question about nesting arrays (although I suppose another potential issue would be a stack overflow by recursing a very deeply nested array)
Carson Myers
If your xml file has 1000 levels, then I'd say your xml file is the problem, not the resulting stack overflow. ;)
Tor Valamo
true, I don't have the xml file though, it comes from a third party
Carson Myers