0

我正在使用 Sphinx 构建搜索引擎,但由于我的 PHP 知识有限,我最终无法显示结果。对搜索服务器的请求给了我这个多维数组:

Array
(
    [0] => Array
        (
            [id] => 327919409
            [weight] => 3
            [attrs] => Array
                (
                    [group_id] => 327919409
                    [date] => 2013
                )

        )

    [1] => Array
        (
            [id] => 84811232
            [weight] => 2
            [attrs] => Array
                (
                    [group_id] => 84811232
                    [date] => 2013
                )

        )

    [2] => Array
        (
            [id] => 150252575
            [weight] => 2
            [attrs] => Array
                (
                    [group_id] => 150252575
                    [date] => 2013
                )

        )

    [3] => Array
        (
            [id] => 174947829
            [weight] => 2
            [attrs] => Array
                (
                    [group_id] => 174947829
                    [date] => 2013
                )

        )

    [4] => Array
        (
            [id] => 297809970
            [weight] => 2
            [attrs] => Array
                (
                    [group_id] => 297809970
                    [date] => 2013
                )

        )

    [5] => Array
        (
            [id] => 391669252
            [weight] => 2
            [attrs] => Array
                (
                    [group_id] => 391669252
                    [date] => 2013
                )

        )

)

我需要检索并列出所有 ID 值,我正在尝试使用此代码,但我得到的只是来自 foreach 的 NULL 值:

$query = $_GET['q'];
$index = "test1";
require_once('sphinxapi.php');
//Sphinx
$s = new SphinxClient;
$s->setServer("localhost", 9312);
$s->setMatchMode(SPH_MATCH_ALL);
$s->SetArrayResult(true);

//Search Query
$result = $s->Query($query, $index);

if ($result['total'] > 0) {

        foreach ($result['matches'] as $key => $id) {
            $ido = $id[$key]->id;
                //Get Column
                $searchColumn = mysql_fetch_array( mysql_query("SELECT * FROM rasti_failai WHERE ID=$ido") );

                //Dump
                var_dump($searchColumn);        
            }


} else {
        echo 'No results found';        
}

任何纠正我的 foreach 循环的帮助将不胜感激。

4

2 回答 2

1

您需要在foreach循环中更改以下行

$ido = $id[$key]->id;

替换为下面

$ido = $id["id"];

您正在遍历数组,并且可以使用方括号访问多维数组,因为您正在使用对象属性的语法。

于 2013-05-06T08:39:09.410 回答
0

似乎问题出在我的 SQL 查询中(?),因为我设法 foreach id 是这样的:

foreach ($result['matches'] as $id) {

    echo $id['id'];
        //$searchColumn = mysql_fetch_array( mysql_query("SELECT * FROM rasti_failai WHERE ID=$ido") );

        //Dump
        //var_dump($searchColumn);       
    }

Anyway as this is not problem with foreach, My question is resolved. Thanks anyways who tried to help

于 2013-05-06T17:46:06.230 回答