>我有一个问题我需要找出数组中键的值是否存在于 PHP 中并获取它的索引值。
示例我有这个数组:
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);
但它不显示搜索的值
现在我需要确定数组中是否存在restaurant_id'1247'?如果存在,请获取该索引。含义是获取索引 1,因为 ID 1247 位于索引 1 中。
我使用了 array_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