我在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”中的内容。我尝试了一些不同的方法,但我无法找到任何方法来做到这一点。请帮帮我。
答案 0 :(得分:2)
foreach ($array['hits']['hits'][0]['_source'] as $key => $value) {
//do stuff
}
答案 1 :(得分:1)
我认为这可能会为你处理。将$ the_array_you_provided替换为您的“main”数组变量(您没有在帖子中指定它)。
$hits = $the_array_you_provided['hits']['hits'];
foreach ($hits as $hit) {
echo $hit['_source']['Title'];
//print everything in the array
//print_r($hit['_source']);
}
任何帮助都可以随意提问。
答案 2 :(得分:1)
试试这个
foreach ($arr['hits']['hits'] as $val)
{
echo $val['_source']['Mob'];
}
像这样
答案 3 :(得分:1)
试试这个:
foreach ($array['hits']['hits'] as $hit) {
foreach ($hit['_source'] as $source) {
echo $source, '<br>';
}
}