0

如果有人对如何解决这个问题有一些帮助/建议,我会非常感激。我创建了一个元搜索引擎,它可以用于所有密集目的,但我的代码很容易破解!以下代码是对象数组的示例 var_dump - searchEngineArray。我想将每个搜索引擎的结果存储在一个数组中,但它需要在某种通用循环中(A)。如果一个搜索引擎不返回结果和 (B),则不会中断。可以轻松容纳其他搜索引擎。

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

我让它在一定程度上工作,但它真的不是很好的代码。这是目前它的工作原理,有没有人对如何创建更通用的信息存储方式有任何想法?

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]"; 
            } */
        }
4

1 回答 1

1
<?
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);
}
?>
于 2012-07-03T12:04:41.363 回答