doukeng7426 2013-05-06 00:38
浏览 90
已采纳

使用Sphinx PHP API从多维数组中检索值

I'm building search engine with Sphinx, but I'm stuck at the end with displaying results as my PHP knowledge is limited. The request to search server gives me this multi dimensional array:

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
                )

        )

)

I need to retrieve and list all ID values, I'm trying to use this code but all I get is NULL values from the foreach:

$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';        
}

Any help correcting my foreach loop would be really appreciated.

  • 写回答

2条回答 默认 最新

  • doufang3001 2013-05-06 17:46
    关注

    It seems the problem was in my SQL query(?) because I've managed to foreach id's like this:

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿