简体   繁体   English

多维数组内部的访问元素

[英]Access elements inside multidimensional array

I have this multidimensional array in PHP: 我在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) "" 
            } 
        } 
    } 
} 

} }

The array has several "hits" inside "hits" and I want to loop trough and print out the stuff inside "_source". 该数组在“ hits”内部有多个“ hits”,我想循环显示槽并打印出“ _source”内部的内容。 I have tried a few different approaches but I cant figure out any way to do this. 我尝试了几种不同的方法,但是我找不到任何方法可以做到这一点。 Please help me. 请帮我。

foreach ($array['hits']['hits'][0]['_source'] as $key => $value) {
    //do stuff
}

I think this may handle it for you. 我认为这可以为您解决。 Replace $the_array_you_provided with your "main" array variable (you've not specified it in the post). 将$ 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']);
}

Any help feel free to ask. 任何帮助都可以随时询问。

Try this 尝试这个

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

}

like this 像这样

Try this: 尝试这个:

   foreach ($array['hits']['hits'] as $hit) {
      foreach ($hit['_source'] as $source) {
         echo $source, '<br>';
      }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM