将组合数组打印到html

时间:2013-07-31 13:54:18

标签: php arrays

我有3个阵列以搜索引擎的漂亮html格式打印出来,这里有用于打印的foreach循环

Bing API

foreach($jsonObj->d->results as $value){
            echo  "<a href=\"{$value->Url}\">{$value->Title}</a><p>{$value->Description}</p>". "<br>";

        }

Blekko API

foreach($js->RESULT as $item){
        echo  "<a href=\"{$item->url}\">{$item->url_title}</a><p>{$item->snippet}</p>". "<br>";

    }

Google API

foreach($all_items as $item){
        echo  "<a href=\"{$item->link}\">{$item->title}</a><p>{$item->snippet}</p>". "<br>";

    }

然后我创建了一个合并的数组,如下面的

$combined = array(); 

foreach($bingArray as $key=>$value){ 
if(isset($combined[$key]))
$combined[$key]["score"] += $value['score']; 
  else
    $combined[$key] = array("score"=>$value['score'],"title"=>$value["title"], "snippet"=>$value   ["snippet"]); 
}

当我执行print_r($ combined)时,我得到以下输出

Array ( [example.com] => Array ( [score] => 51 [title] => example title[snippet] => Blah baly... )[example2.com] => Array ( [score] => 45 [title] => example title2[snippet] => Blah baly2... ) ....) 

这很好,并且格式与所有3个API数组相同,现在我正在尝试打印html中的组合数组,就像3 API一样,这里是我试过的代码

foreach($combined as $value){
            echo  "<a href=\"{$value->url}\">{$value->title}</a><p>{$value->snippet}</p>". "<br>";

            }

但是当我运行它时,我得到这个错误“试图获取非对象的属性”,我怀疑我需要在这里更改某些东西“foreach($ combined as $ value)”但我不确定是什么,可以任何帮助

1 个答案:

答案 0 :(得分:0)

这是因为你不再拥有对象了。

改变这个:

foreach($combined as $value){
    echo "<a href=\"{$value->url}\">{$value->title}</a><p>{$value->snippet}</p>". "<br>";
}

对此:

foreach($combined as $url => $value){
    echo "<a href=\"{$url}\">{$value['title']}</a><p>{$value['snippet']}</p>". "<br>";
}