I'm building out a careers page that reads the available positions from a hosted json file. I'm having difficulty getting the data to filter correctly by more than on value.
This example tries to output only the matching values from the $data array when comparing with key/values set in the $filter array:
<?php
/* ----------------------
Dump the output
---------------------- */
function dump($data) {
if(is_array($data)) { //If the given variable is an array, print using the print_r function.
print "<pre>\n";
print_r($data);
print "</pre>";
} elseif (is_object($data)) {
print "<pre>\n";
var_dump($data);
print "</pre>";
} else {
print "=========> ";
var_dump($data);
print " <=========";
}
}
/* ----------------------
Sanitize a string to url friendly
---------------------- */
function cleanString($str) {
$removetags = array("'", '"', ',', '.', '?', '& ', '& ', '/', '#', '@','(',')');
$removespace[] = ' ';
$notags = str_replace($removetags,"",$str);
$nospaces = strtolower(str_replace($removespace,"-",$notags));
return preg_replace('~-{2,}~', '-', $nospaces);
}
function sanitizeString($val) {
if(is_array($val)) {
$array = array() ;
foreach ($val as $key => $value) {
$array[$key] = cleanString($value);
}
return $array;
} else {
$result = cleanString($val);
}
return $result;
}
/* ----------------------
Filter the array data, should match all values;=
---------------------- */
function filterData($array, $filter) {
$result = array();
foreach ($filter as $gk => $gv) {
foreach ($array as $key => $value) {
if (array_key_exists($gk, $value) && !empty($value[$gk])) {
if(is_array($value[$gk])) {
$child = array_search($gv, sanitizeString($value[$gk]));
if(!empty($value[$gk][$child])) {
$theValue = sanitizeString($value[$gk][$child]);
if ($theValue === $gv ) {
array_push($result,$value);
}
}
} else {
$theValue = sanitizeString($value[$gk]);
if ($theValue === $gv ) {
array_push($result,$value);
}
}
}
}
}
return (!empty($result)) ? $result : $array;
};
// sample data
$data = '{"jobs":[{"department":"sales","location":{"country":"United States","country_code":"US","region":"New York","region_code":"NY","city":"New York","zip_code":null,"telecommuting":false}},{"department":null,"location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"Leeds","zip_code":null,"telecommuting":false}},{"department":"Project Management","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"Manchester","zip_code":null,"telecommuting":false}},{"department":"Project Management","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}},{"department":"Customer Success","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}},{"department":null,"location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}}]}';
$decode = json_decode($data, true);
// sample filter - values should be lowercase, and sanitised;
$filter = array (
'department' => 'project-management',
'location' => 'london'
);
$filterdata = filterData($decode['jobs'], $filter);
$i = 1;
foreach ($filterdata as $res) {
echo $i . '<br />';
echo $res['department'] . '<br />';
echo $res['location']['city'] . '<hr />';
$i++;
}
?>
What I'm finding is that the filterData function will return results that match any set key/value in the $filter array. So I can get duplicate entries if a department and location are both matched.
How do I go about adjusting this so that I only get the array output that where all $filter key/values match?
Thanks - I've spent a day on this and am no futher, so any help in the right direction would be greatly appreciated.