doukeng7426 2013-05-05 16: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 09: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部