dtqysxw4659 2016-12-14 21:19
浏览 53
已采纳

php如何获取数组数组的键值

I have the following setup where I'm trying to use an array of array structure. I'm not sure how to get the key value once the value is found in the array of arrays.

$testboat = 'smallest boat';
$allboats = array(40=>array(1=>'big boat',
                            2=>'bigger boat'
                      ),
                  30=>array(1=>'little boat',
                           2=>'tiny boat',
                           3=>'smallest boat'));

foreach($allboats as $boats){
    foreach($boats as $boat){
       if($testboat == $boat) {

       /*looking to echo the key or value 30; */

      }  

   }
}
  • 写回答

3条回答 默认 最新

  • douyuan4697 2016-12-14 21:32
    关注

    Use the $key => $value syntax of foreach(). Also, no need to loop through the inner arrays:

    foreach($allboats as $key => $boats){
        if(in_array($testboat, $boats)) {
            echo $key;
            break; //if you want to stop after found
        }
    }
    

    If you want to get the outer key and the inner key:

    foreach($allboats as $key => $boats){
        if(($inner_key = array_search($testboat, $boats)) !== false) {
            echo "$key and $inner_key";
            break; //if you want to stop after found
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击