Reputation: 127
I have a multidimensional array and I would like to search with 2 parameters : city and zip.
My array :
$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"
}
}
Ex : search the key value for city=paris and zip=75100
Key=1
An idea ?
Upvotes: 0
Views: 75
Reputation: 2935
Check this,
<?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;
}
}
?>
Upvotes: 0
Reputation: 3536
Use this function..
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';
}
Upvotes: 1