0

我在 PHP 中有这个多维数组:

array(4) { 
["took"]=> int(2) 
["timed_out"]=> bool(false) 
["_shards"]=> array(3) { 
    ["total"]=> int(5)
    ["successful"]=> int(5)
    ["failed"]=> int(0) 
} 
["hits"]=> array(3) { 
    ["total"]=> int(3) 
    ["max_score"]=> float(2.3578677) 
    ["hits"]=> array(1) { 
        [0]=> array(5) { 
            ["_index"]=> string(13) "telephonebook" 
            ["_type"]=> string(6) "person" 
            ["_id"]=> string(22) "M5vJJZasTGG2L_RbCQZcKA" 
            ["_score"]=> float(2.3578677) 
            ["_source"]=> array(8) { 
                ["Mob"]=> string(19) "XXX" 
                ["Title"]=> string(13) "Analyst" 
                ["Department"]=> string(8) "Analysis" 
                ["Country"]=> string(6) "Sweden" 
                ["Tlf"]=> string(0) "" 
                ["Name"]=> string(16) "XXX" 
                ["Email"]=> string(29) "XX@retro.com" 
                ["thumbnailPhoto"]=> string(0) "" 
            } 
        } 
    } 
} 

}

该数组在“命中”中有几个“命中”,我想循环并打印出“_source”中的内容。我尝试了几种不同的方法,但我想不出任何方法来做到这一点。请帮我。

4

4 回答 4

2
foreach ($array['hits']['hits'][0]['_source'] as $key => $value) {
    //do stuff
}
于 2013-04-17T13:04:13.817 回答
1

我想这可能会为你处理。将 $the_array_you_provided 替换为您的“主”数组变量(您没有在帖子中指定它)。

$hits = $the_array_you_provided['hits']['hits'];

foreach ($hits as $hit) {
    echo $hit['_source']['Title'];

    //print everything in the array
    //print_r($hit['_source']);
}

任何帮助随时询问。

于 2013-04-17T13:07:22.447 回答
1

试试这个

foreach ($arr['hits']['hits'] as $val) 
{
   echo $val['_source']['Mob'];

}

像这样

于 2013-04-17T13:07:25.787 回答
1

试试这个:

   foreach ($array['hits']['hits'] as $hit) {
      foreach ($hit['_source'] as $source) {
         echo $source, '<br>';
      }
   }
于 2013-04-17T13:10:08.007 回答