How best to loop through an array yet maintain another array alongside
<p>I have 1 array that contains 4 elements.  These categories in this array are in a hierarchy such that category2 can have multiple instances in category1, category3 could have multiple instances in category2, etc.</p>

<p>I have a web scrape function that has an array argument that I call for each category.  The function returns an array called data that contains an array of the actual instances of the category as well and a varying number of string elements that have to be used in subsequent scrape calls to the succeeding categories.  In order to get down to the data that I'm looking for I have to scrape category1, input that output into a category2 scrape, input that output into a category3 scrape, etc until I finally end up with the large dataset at the end of category4.</p>

<p>However, each element of the dataset array that each category returns has to also go through this scrape function.  It's a crazy many->many hierarchical mess that I have to maintain order all the way down through the tree.</p>

<p>I also have to maintain a breadcrumb trail of sorts for each instance as the loop progresses.  For example, scrape category1, category1 has 3 instances, category2 has 10 instances and category3 has 25 instances.  I scrape category1 to find it has 3 instances.  I then take the values of each of these three instances and feed them separately into each scrape for category2.  If this is successful, I then have to use each of the original 3 category1 instance values as well as the values for the 10 instances of category2 and feed them to category3.</p>

<p>Here's an example of the code I'm working with:</p>

<pre><code>$aCategoryOrder = array('category1','category2','category3','category4');

## First category web scrape ONLY needs the category name as a param
$aCatInput['category'] = $aCategoryOrder[0];

for ($i = 0; $i &lt; count($aCategoryOrder); $i++) {
    $sThisCat = $aCatInput['category'];
    $sNextCat = $aCategoryOrder[$i + 1];

    if ($i == 0) {
        $aCatInput['category'] = $sThisCat;
    } else {
        $aCatInput['category'] = $sNextCat;
    }##endif

    $aScrape = webScrape($aCatInput);

    ## Capture the dataset out into another array to display later labeled 
    ## with the category name.
    $aOutput[$sThisCat]['data'] = $aScrape['data'];


    ## If this isn't category4 then we have to capture some of the output of the 
    ## web scrape to use for the next category's web scrape input.
    if ($aCatInput['category'] &lt;&gt; end($aCategoryOrder)) {

        ## Assign the name of the next category
        $aCatInput['category'] = $sNextCat;

        ## Assign the view state to the next category
        $sNextViewState = $aScrape['view_state'];
        $aCatInput['view_state'] = $sNextViewState;

        $sValName = $sThisCat . '_val';

        ## Separate out the actual data we're going to loop through
        $aCatData = $aScrape['data'];

        ## This is where my problem is.  Using this webScrape function again drives me
        ## nuts here.  To loop through all of these instances is the exact same code 
        ## above.  However, I have to pass 1 to many different instances through it.
        ## There could be 40 or more elements in $aCatData that I need to run through
        ## the webScrape function.  I'm trying to get away from having nested loops.
        foreach ($aCatData as $sVal) {
            $aCatInput[$sValName] = $sVal;          
            $aScrape = scrapeBookstore($aCatInput);
            $aOutput[$sNextCat]['data'][] = $aScrape['data'];
            $aCatInput['view_state'] = $aScrape['view_state'];
        }##endforeach
    }##endif    
}##endfor
</code></pre>

<p>That is what I have so far.  I believe this is a good application for a recursive function but I just can't think this whole thing through in my head to get any reliable solution.</p>
