我有一个看起来像这样的数组:
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
)
)
答案 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');