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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)