我有一个多维数组,我想用2个参数搜索:city和zip。
我的阵列:
$result=array(2) {
[0]=> array(2)
{
["city"]=> string(9) "Paris"
["zip"]=> string(5) "75000"
}
[1]=> array(2)
{
["city"]=> string(9) "Paris"
["zip"]=> string(5) "75100"
}
}
例如:搜索city = paris和zip = 75100
的键值关键= 1
一个想法?
答案 0 :(得分:1)
使用此功能..
function find_key($city, $zip) {
foreach($result as $key => $value) {
if($value['city'] == $city && $value['zip'] == $zip) {
return $key;
}
}
return FALSE;
}
$key = find_key('paris','75100');
if($key) {
echo 'Found at key:'.$key;
} else {
echo 'Not Found';
}
答案 1 :(得分:0)
检查一下,
<?php
$result=array(
array( "city"=> "Paris", "zip"=>"75000"),
array( "city"=> "Paris", "zip"=>"75100")
);
$searchCity = "Paris";
$searchZip = "75100";
foreach($result as $key=>$row){
if($row['city'] == $searchCity && $row['zip'] == $searchZip){
echo $key;
}
}
?>