Skip to content
Vladimir Jimenez edited this page Jun 5, 2015 · 3 revisions

Fetching a dataset is simple, but do you really want all of the results...? Well, maybe you do.

If you don't, the Socrata API allows you to filter the results or write SQL-like queries to better refine your results and all of this is supported by PhpSoda.

Using Simple Filters

Socrata supports simple filters to easily filter results based on the values of a specific column. For example, if a dataset contains information from the different states but you only want results from Arkansas (AR), you would use a simple filter.

PhpSoda supports simple filters with two methods, either building the simple filter as a string or giving it an associative array.

A Simple Filter as a String

If you would like to build the simple filter as a string, you must format it as a URL query with equal signs and ampersands for multiple values. In our example we want results from just Arkansas so we'd use state=AR.

But if you wanted results specifically from Little Rock, Arkansas, you could use state=AR&city=Little Rock.

Warning: Creating a simple filter using a string requires that you urlencode() your filter as PhpSoda does not do this for string simple filters. It is recommended you use an associative array instead.

Example
$simpleFilter = "state=AR";

$results = $ds->getDataset($simpleFilter);

A Simple Filter as an array

We'll be using the sample example from the previous section, but will be creating our simple filter as an array. The key and value of the associative array correspond to the URL key/value pairs that are used in URLs.

Using the previous example, we want results from Little Rock, Arkansas.

$arrayFilter = array(
    "state" => "AR",
    "city"  => "Little Rock"
);

Notice: Unlike the string simple filters, values passed in an associative array are automatically urlencoded.

Example
$arrayFilter = array("state" => "AR");

$results = $ds->getDataset($arrayFilter);