SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    0 Post(s)
    0 Thread(s)

    Iterating through nested arrays


    I'm trying to create a data structure consisting of nested associative arrays.

    It is created by:

    1. Taking an array from a query result using mysql_fetch_array($result, MYSQL_ASSOC).
    2. Then iterate through this array. Each attribute name in this array is compared to a lookup table which will determine whether the attribute value references another table, i.e. the value is a code that is the primary key of another table.
    3. For every attribute value that is a code that has a table to reference, fetch the data and replace the original string value (in the first array) with this new array.

    And so on.

    There could be anything from 0 to 5 levels to fetch referenced tuple data for, so I've been trying to come up with a recursive function to do this, but have failed so far.

    Does anybody have any tips for iterating through such a simple structure, and applying a function to each member like the above; or maybe a link to an article that has a strategy?

    I am of course not looking for code from you, I'm probably just missing some convenient PHP construct for doing this.

    I've already had a look at array_walk_recursive : PHP manual : array_walk_recursive

    but I can't see how I could use this, as although it would be great to get the information out, am I wrong in thinking it's not so great for the creation of the above data structure?

    Any thoughts on this would be greatly appreciated.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    0 Post(s)
    0 Thread(s)
    Seems you have a kind of tree-like structure. There are better methods to handle it than recursive queries. Find a site point article about this (keyword: nested sets).


Posting Permissions

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