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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,