I'm having trouble filtering an array.
I'm passing a set of features in the URL, and I want to match items from my array that have all the specified features. As soon as I remove a parameter from the URL the code falls apart as I'm using $_GET
to filter. I've gone ahead and created an array for debugging purposes. I also provided the the get parameters as a comment in the code. Can some one please help me out here? Please feel free to copy the code to your IDE.
The following is my code:
<?php
error_reporting(E_ALL & ~E_NOTICE);
// Created a static array for you guys for debugging
// page.php?pool=1&beach=1&concierge=1 put this url path at the end of the url so you can get the data im seeing
$htls=array(
array("HotelName"=>"M Beach Hotel","minprice"=>264,"CountryCode"=>"US",'feature'=>array("type"=>"hotel","pool"=>1,"beach"=>1,'concierge'=>0)), array("HotelName"=>"Hilton","minprice"=>73,"CountryCode"=>"US",'feature'=>array("type"=>"hotel","pool"=>1,"beach"=>0,'concierge'=>1)),
array("HotelName"=>"Fontainebleau","minprice"=>375,"CountryCode"=>"US",'feature'=>array("type"=>"resort","pool"=>1,"beach"=>1,'concierge'=>1)),
array("HotelName"=>"Woodlow Inn","minprice"=>40,"CountryCode"=>"US",'feature'=>array("type"=>"inn","pool"=>0,"beach"=>0,'concierge'=>0)),
array("HotelName"=>"El cabanna","minprice"=>73,"CountryCode"=>"US",'feature'=>array("type"=>"resort","pool"=>1,"beach"=>1,'concierge'=>1)),
array("HotelName"=>"James south beach","minprice"=>73,"CountryCode"=>"US",'feature'=>array("type"=>"hotel","pool"=>0,"beach"=>1,'concierge'=>0)),
);
foreach($htls as $key1 => $val ){
foreach($val['feature'] as $key => $val2){
// Here's where I start to get lost soon as a parameter is not in the url for example ?pool=1 the code falls apart.
if ($_GET['pool']==1 && $val['feature']['pool']==1 and $_GET['beach']==1 && $val['feature']['beach']==1 and $_GET['concierge']==1 && $val['feature']['concierge']==1)
{
echo $key.'- '.$val2.' - '.$val['feature']['pool'].' - '.$val['HotelName'].'<br>';
}
break;
}
}
?>