Logical Nonsense
Logical Nonsense

Reputation: 413

How to filter data in an array with a set of criteria from $_GET

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;  
    }
}
?>

Upvotes: 2

Views: 216

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

If you're trying to match items from your hotels array that have all of the set of features included in the query string, you can filter your array using array_diff_assoc to compare the array of features in $_GET with the array of features of the hotel record.

$filtered = array_filter($htls, function($hotel) {
    return !array_diff_assoc($_GET, $hotel['feature']);
});

array_diff_assoc will return an array of any key/value pairs in $_GET that aren't included in $hotel['feature']. Negating it with ! will implicitly cast it to boolean, so the callback will return true if it's empty and false if it's not.

This just does the filtering. After they're filtered you can iterate $filtered and output them however you need to.

If there are other parameters besides the filters in $_GET, you can rename your filter controls with an array format to group them together, so you can refer to $_GET['filters'] instead of just $_GET. I'm guessing these values may come from checkboxes, so it would be like this:

<input type="checkbox" name="filters[pool]" value="1">

Executable example at 3v4l.org.

Upvotes: 2

Related Questions