狮身人面像返回阵列和重量。

时间:2013-02-24 19:16:43

标签: php arrays sphinx

我试图简单地回应所返回的重量并做一些非常错误的事情。

以下是返回数据的示例:     排列     (         [错误] =>         [警告] =>         [status] => 0         [fields] =>排列           (             [0] =>短语           )        [attrs] =>排列         (         )

   [matches] => Array
     (
        [34] => Array
            (
                [weight] => 1707
                [attrs] => Array
                    (
                    )

            )

    )

所以这是获取id的工作代码。这对我来说很有意义。

      if ( ! empty($result["matches"]) ) {
      foreach ( $result["matches"] as $doc => $docinfo ) {
            echo "DOC ID:$doc\n";
      }

      print_r( $result );
  }

我尝试了另一个使用doc id的foreach然后循环并获得权重..

foreach $doc as $weight  {

不起作用。我猜这里我用多维数组做错了但是我不确定如何解决嵌套数组...

由于

1 个答案:

答案 0 :(得分:1)

你要尝试递归函数

$array =array(
    "matches" => array(
        array("weight"=>1707,"attrs"=>array("weight"=>100)),
        array("weight"=>1700,"attrs"=>array("weight"=>170)),
        array("weight"=>10,"attrs"=>array("weight"=>170,"attr"=>array("weight"=>"something other")))
    )
);

function recurse($array){
    foreach ($array as $arr){
        if (is_array($arr)){
            recurse($arr);
        } else {
            echo $arr . "<br>";
        }
    }
}

recurse($array);

Note:此函数将返回来自任何级别数组的每个weight参数值

<强>已更新

如果您想从以下数组输出weight参数值,请尝试使用

$array =array(
    "matches" => array(
        array("weight"=>1707,"attrs"=>"other param"),
        array("weight"=>1700,"attrs"=>"something other too"),
    )
);

foreach($array['matches'] as $arr){
    echo $arr['weight'] . "<br>";
}