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) ""
}
}
}
}
}
数组在"hits"里面有几个"hits",我想循环并打印出"_source"里面的东西。我试过几种不同的方法,但我想不出任何方法来做到这一点。
foreach ($array['hits']['hits'][0]['_source'] as $key => $value) {
//do stuff
}
试试这个
foreach ($arr['hits']['hits'] as $val)
{
echo $val['_source']['Mob'];
}
这样的我想这个可以解决你的问题。将$the_array_you_provided替换为你的"main"数组变量(你没有在post中指定它)。
$hits = $the_array_you_provided['hits']['hits'];
foreach ($hits as $hit) {
echo $hit['_source']['Title'];
//print everything in the array
//print_r($hit['_source']);
}
如果有任何帮助,请随时询问。
试试这个:
foreach ($array['hits']['hits'] as $hit) {
foreach ($hit['_source'] as $source) {
echo $source, '<br>';
}
}