PHP获取多维数组键值

时间:2013-03-10 02:04:42

标签: php arrays multidimensional-array

我有一个看起来像这样的数组:

Array
(
    [100] => Array
        (
            [room_id] => 100
            [name] => Town Center
        )

    [110] => Array
        (
            [room_id] => 110
            [name] => Coffee Shop
            [pin_id] => 7146
            [pin_x] => 570
            [pin_y] => 150
        )
)

我想知道是否有一种方法可以找到特定值的密钥,如果“pin_id”值存在的话。例如:

Array
(
    [100] => Array
        (
            [room_id] => 100
            [name] => Town Center
        )

    [110] => Array                    <- I want to get this key "110"             
        (                                 
            [room_id] => 110          
            [name] => Coffee Shop     
            [pin_id] => 7146          <- Because "pin_id" is here
            [pin_x] => 570
            [pin_y] => 150
        )
)

1 个答案:

答案 0 :(得分:1)

您必须通过循环传递数组并使用array_key_exists

function makeHappen($argument){
  //Array
    foreach ($argument as $val) {
      if(array_key_exists($val, $array)){
            //if key exists, do something
      }
    }
}
echo makeHappen('pin_id');