如何使用PHP找出数组中是否存在键值?
我有个问题 我需要找出数组中键的值是否存在于PHP中,并获取它的索引值 例如,我有一个数组:如何使用PHP找出数组中是否存在键值?,php,arrays,Php,Arrays,我有个问题 我需要找出数组中键的值是否存在于PHP中,并获取它的索引值 例如,我有一个数组: Array ( [0] => Array ( [restaurant_id] => 1519 [new_lat] => 14.63807 [new_long] => 121.03158 [date_updated] => 2013-11-14 16:40:
Array
(
[0] => Array
(
[restaurant_id] => 1519
[new_lat] => 14.63807
[new_long] => 121.03158
[date_updated] => 2013-11-14 16:40:34
)
[1] => Array
(
[restaurant_id] => 1247
[new_lat] => 14.63877
[new_long] => 121.03265
[date_updated] => 2013-11-14 16:48:41
)
)
我还使用了以下代码:
$key = array_search($data_add['restaurant_id'],$data);
但它不显示搜索的值
现在我需要确定数组中是否存在id为“1247”的餐厅?如果存在,请获取该索引。意思是获取索引1,因为ID1247在索引1中
我使用了数组_key_exists(),但它会找到键而不是键值。我希望你能帮助我,谢谢。试试这个:
$myArray = []; // Array as above
$target = '1247';
for ($i=0; $i<count($myArray);$i++) {
if ($myArray[$i]['restaurant_id'] == $target) {
break;
}
}
// $i contains index of target
$myArray=[];//数组如上所示
$target='1247';
对于($i=0;$i)