Search code examples
phparraysmeta-search

accessing objects data in an array


If someone has some help/advice about how to tackle this problem I'd really appreciate it. I've created a metasearch engine that for all intensive purposes works ok, but my code is pretty breakable! The following code is a sample var_dump of the array of objects - searchEngineArray. I want to store the results of each search engine in an array, but it needs to be in some kind of generic loop that (A). won't break if one search engine doesn't return results and (B). can easily accommodate additional search engines.

object(BingSearch)[1]
  private 'formatted_query' => string 'england' (length=7)
  public 'search_results' => 
    array
      0 => 
        array
          'title' => string 'England - Wikipedia, the free encyclopedia' (length=42)
          'url' => string 'http://en.wikipedia.org/wiki/England' (length=36)
          'score' => int 30
      1 => 
        array
          'title' => string 'BBC News - England' (length=18)
          'url' => string 'http://www.bbc.co.uk/news/england/' (length=34)
          'score' => int 28
      2 => 
        array
          'title' => string 'The FA - The website for the English football association, The FA ...' (length=69)
          'url' => string 'http://www.thefa.com/' (length=21)
          'score' => int 26

object(BlekkoSearch)[2]
  private 'formatted_query' => string 'england' (length=7)
  public 'search_results' => 
    array
      0 => 
        array
          'title' => string '<strong>England</strong> - Wikipedia' (length=36)
          'url' => string 'http://en.wikipedia.org/wiki/England' (length=36)
          'score' => int 25
      1 => 
        array
          'title' => string 'The official site of Visit <strong>England</strong> - The <strong>England</strong> Tourist Board' (length=96)
          'url' => string 'http://www.enjoyengland.com/' (length=28)
          'score' => int 23
      2 => 
        array
          'title' => string 'Arts Council <strong>England</strong> - Arts Council' (length=52)
          'url' => string 'http://www.artscouncil.org.uk/' (length=30)
          'score' => int 21


object(EntirewebSearch)[3]
  private 'formatted_query' => string 'england' (length=7)
  public 'search_results' => 
    array
      0 => 
        array
          'title' => string 'Arts Council England | Arts Council' (length=35)
          'url' => string 'http://www.artscouncil.org.uk/' (length=30)
          'score' => int 20
      1 => 
        array
          'title' => string 'Sport England ' (length=14)
          'url' => string 'http://www.sportengland.org/' (length=28)
          'score' => int 18
      2 => 
        array
          'title' => string 'Bank of England ' (length=16)
          'url' => string 'http://www.bankofengland.co.uk/' (length=31)
          'score' => int 16

I have it working to an extent, but its just not good code really. Heres how it works at the moment, has anyone got any ideas on how to create a more generic way of storing the information?

public function storeResults($searchEnginesArray)
    {
        //The following is very bad
        //$blekko_Array = "";
        //$bing_Array = "";
        //$entireweb_Array = "";

        for($x=0; $x<sizeof($searchEnginesArray); $x++)
        {
            var_dump($searchEnginesArray[$x]);
            /*switch ($searchEnginesArray[$x]->getEngineName()) {
            case "Bing":
                $bing_Array = $searchEnginesArray[$x]->getResults();
                break;
            case "Blekko":
                $blekko_Array = $searchEnginesArray[$x]->getResults();
                break;
            case "Entireweb":
                $entireweb_Array = $searchEnginesArray[$x]->getResults();
                break;
            default:
                echo "Error: Unexpected Search Engine : ".$searchEnginesArray[$x]->getEngineName(). " Expects [Bing/Blekko/Entireweb]"; 
            } */
        }

Solution

  • <?
    public function storeResults($searchEnginesArray) {
        for($i=0;$i<count($searchEnginesArray);$i++) {
            $result = $searchEnginesArray[$i]->getResults();
            // Ignore engines with empty results
            if( !empty($result) ) {
                $results[ $searchEnginesArray[$i]->getEngineName() ] = $result;
            }
        }
        // create an array for every engine, are you sure it's needed?
        extract($results, EXTR_SKIP);
    }
    ?>