将数组显示为搜索结果?

时间:2014-08-07 05:56:17

标签: php arrays

我有一个PHP脚本,可以将搜索结果显示为数组。以下是我目前作为输出显示的示例:

[0] => Array
    (
        [idx] => 0
        [title] => Stack Overflow
        [description] => A language-independent collaboratively edited question and answer site for programmers.
        [url] => http://stackoverflow.com/
    )

[1] => Array
    (
        [idx] => 1
        [title] => Stack Overflow - Wikipedia, the free encyclopedia
        [description] => Stack Overflow website logo.png · Stack Overflow.png. Screenshot of Stack Overflow as of December 2011. Web address · stackoverflow.com. Commercial? Yes.
        [url] => http://en.wikipedia.org/wiki/Stack_Overflow
    )

我想要做的是在PHP页面上显示它,使其看起来像普通的搜索结果。对于上面的例子,它看起来像这样:

1.  <a href="http://stackoverflow.com/">Stack Overflow</a>
A language-independent collaboratively edited question and answer site for programmers.

2.  <a href="http://en.wikipedia.org/wiki/Stack_Overflow">Stack Overflow - Wikipedia, the free encyclopedia</a>
Stack Overflow website logo.png &middot; Stack Overflow.png. Screenshot of Stack Overflow as of December 2011. Web address &middot; stackoverflow.com. Commercial? Yes.

对此的任何帮助都将受到高度赞赏。

4 个答案:

答案 0 :(得分:0)

您可以使用键迭代数组并访问内部数组属性,请注意您应确保填充数组,您可以使用isset检查是否存在密钥:

foreach($searchResult as $index => $result) {
  echo $result["idx"] . ". <a href=" . $result["url"] . ">" . $result["title"] . "</a>";
  echo $result["description"]
}

答案 1 :(得分:0)

使用以下循环打印 -

foreach($result as $key => $value) {
  echo "<div>", $value['idx'], ". ";
  echo "<a href='{$value['url']}'> {$value['title']} </a>";
  echo "<p>{$value['description']}</p>";
  echo "</div>";
}

答案 2 :(得分:0)

<?php
    $result = "";
    foreach($your_array as $key=>$value){
        $result .= $value["idx"] . ". <a href=" . $value["url"] . ">" . $value["title"] . "</a><br/><p>" . $value["description"] . "</p><br/>";
    }
    echo $result;
?>

答案 3 :(得分:0)

foreach($your_search_array as $val){
    echo $val['idx']+1.'. <a href="'.$val['url'].'">'.$val['title'].'</a> '.$val['description'];
}