Results 1 to 6 of 6

Thread: Arrays with Dynamic Dimensions

  1. #1

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Arrays with Dynamic Dimensions

    What is the easiest way to see how many dimensions an array has?

    The reason is, I'm trying to create a sort of node tree, and as my data parser reads deeper in a file, it needs to add to an array like this:

    PHP Code:
    $node[0] = "a node"
    then, the next child would be:

    PHP Code:
    $node[0][0] = "a child"
    ...and eventually I'm pretty far from home:

    PHP Code:
    $node[3][0][0][5] = "some pretty deep data"
    Since there is no way to tell the number of children that a file may generate in parsing, at times I need to find out how deep I am, achieving the code snippets above?
    Last edited by tomcatexodus; Apr 6th, 2010 at 12:55 AM.
    IWS

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Arrays with Dynamic Dimensions

    you would probably need to create a recursive function that just looped through it all, I guess, unless you know specifically which node and child you're inside of. if you are interested in how deep you are at this moment while you're reading the file, then that is much different than just seeing how deep the array goes in general. if you were interested in how deep it is at that moment, then could I assume that the last indexes added to the array are the ones you're most interested in? like, in your example for the "pretty deep data," the "most recent" addition would be the fourth node -> first child -> first grandchild -> sixth great grandchild: correct? if so, then all you'd really need to do in this recursive function is find the last index and have a static variable that increments every time the function returns (because I assume you want an integer return of how many "levels" you have).

    hopefully that makes some sense to you -- I have to go right away, so I can't leave you any code. I'll make a new post in just a little while hopefully and might be able to provide you with something to start you off.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Arrays with Dynamic Dimensions

    so, I went to start something but then realized this would be sort of a waste of time if I didn't have more information from you. if you need more help than the short explanation I gave before, post some of your code so that I can see what you're doing first! that would make things much easier for me, too.

  4. #4

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Re: Arrays with Dynamic Dimensions

    Perhaps my approach is not the most appropriate, I'm seeing holes open up in the logic quite quickly.

    What I'm trying to achieve (so you know where this is coming from) is a simple node tree that mimics the HTML DOM. I've created a regex that reads an HTML string, and can extract the node name, attributes, child node data and sibling node data. The regex parser stores child and sibling data, and then works it's way deeper into the node tree, buffering the sibling data in an array. When it runs out of children, it pops the next set of sibling data off the array, and then parses it and any children. This repeats until there are no children and no siblings buffered.

    My problem though, is tracking the node I'm currently on through each recursion.

    Instead, what would be a less complicated method to map a node tree?
    IWS

  5. #5
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Arrays with Dynamic Dimensions

    Would it be more appropriate to use something like the DOMDocument class? Here is an example of simple usage with XML.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Arrays with Dynamic Dimensions

    I would suggest DOMDocument too — but I am not sure how it handles malformed markup or different character encodings.

    Your explanation of your logic seems to make sense. We would have to see the code to make any suggestions for improvement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width