I have a bit of code that works, but runs very, very slow. I think the thing that's killing me is the double nested foreach() loops, but it may be something else entirely. The original code is as follows:

  foreach($pubsArr['website'] as $website)
    {
    $websiteArr[]=array('name' => $website['title'],
                        'desc' => $website['description'],
                        'url' => $website['link'],
                        'org' => $website['organization']);
    $siteFacetList=array();
    foreach($website['facet'] as $facet)
      {
      $fname=$facet['name'];
      if($facet[0]==array() && trim($fname) != 'Product Format')
        {
        if(!isset($siteFacetList[$fname]))
          {
          $siteFacetList[$fname]=array();
          }
        if(is_array($facet['term']))
          {
          foreach($facet['term'] as $fterm1)
            {
            if(!isset($siteFacetList[$fname][$fterm1]))
              {
              $siteFacetList[$fname][]=$fterm1;
              }
            }// end foreach($fterm as $fterm1)
          }// end if(is_array($fterm))
        else
          {
          if(!isset($siteFacetList[$fname][$facet['term']]))$siteFacetList[$fname][]=$facet['term'];
          }
        }// end if($facet[0]==array() && trim($facet['name']) != 'Product Format')
      }// end foreach($website['facet'] as $facet)
    $websiteArr[$websiteCount]['filters']=$siteFacetList;
    unset($siteFacetList);
    $websiteCount++;
    }//end foreach($pubsArr['website'] as $website)

Can someone provide a better structure or technique than what I'm using? Any help would be appreciated.

I have tried the following replacing foreach() with for() loops, but there was no discernable time difference. Oddly, replacing !isset() with !in_array() actually doubled my execution time...

    It looks like you are simply dealing with lots of data. How much data? how slow is "very, very slow"? what are the actual input/output formats you need?

    jkurrle;11037843 wrote:

    Oddly, replacing !isset() with !in_array() actually doubled my execution time...

    That's because [man]isset[/man] is a language construct, while [man]in_array[/man] is a function.

      It might also be relevant to note that this:

      if(!isset($siteFacetList[$fname][$facet['term']]))$siteFacetList[$fname][]=$facet['term'];

      doesn't make much sense. Unless $facet['term'] is a series of monotonically increasing 0-based integers, you're checking to see if one array key exists and, if not, adding a value to the array under a different key.

        Write a Reply...