Filter array data output using one or more matched criteria

I’m trying to filter a multidimensional array saved in a json file to output either all, none or just the arrays where one or more key values match each criteria set been declared in an array ($filter).

Some example filter array configurations could be:

department = marketing

The result should be all the jobs in the marketing department, or return an error for an empty array

department = marketing & location = london

The result should show all marketing jobs in london, or return an error for an empty array

location = london

The result should show all jobs in london, or return an error for an empty array

So the above describes my expected output, but what i find is the results will return any jobs for each filter criteria.

This can result in returning any jobs in a both department and location, as well as duplicate entries where all criteria are met.

This is my code. For the purpose of demoing the issue, I’ve output a basic example of the json file to the $data array.

<?php

	$data = array(
		0 => array (
			'id' => 1,
			'department' => 'web',
			'location' => 'london'
		),
		1 => array (
			'id' => 2,
			'department' => 'marketing',
			'location' => 'london'
		),
		2 => array (
			'id' => 3,
			'department' => 'web',
			'location' => 'leeds'
		),
		3 => array (
			'id' => 4,
			'department' => 'marketing',
			'location' => 'leeds'
		),
		
	);
		
	$filter = array (
		'location' => 'london',
		'department' => 'marketing'
	);
	
	
	function check_data($arr, $k, $v) {
		
		if (array_key_exists($k, $arr) && $arr[$k] === $v ) {
	
			return $arr;
	
		}
	
	}
	
	
	function filter_data($data, $filter) {
		
		$result = array();
		
		foreach($filter as $fk => $fv) {
			
			foreach ($data as $key => $value) {
				
				if (check_data($value, $fk, $fv) && check_data($value, $fk, $fv)) {
					
					$result[] = check_data($value, $fk, $fv);
						
				}
	
			}
	
		}
	
		if(empty($filter)) {		
			return ($data);
		}
		
		return (empty($result)) ? 'error' : $result;
		
	}
	
	
	$results = filter_data($data, $filter);
	
	echo '<pre>';
	var_dump($results);
	echo '</pre>';
	
	if($results === 'error') {
		print '<h1>No Results</h1>';
	}

If i set just one criteria in the $filter array i get the expected output. :slight_smile:

$filter = array (
	'location' => 'london'
);

If i set more than one criteria in the $filter array i get results for both criteria :frowning:

$filter = array (
	'location' => 'london',
	'department' => 'marketing'
);

If i set the value of one or more of multiple criteria to a value that won’t match, i still get results, rather than an error. :frowning:

$filter = array (
	'location' => 'beep',
	'department' => 'marketing'
);

If i set all values for each criteria to value that won’t match, i get the expected error :slight_smile:

$filter = array (
	'location' => 'beep',
	'department' => 'book'
);

Have i missed something, I’m not sure how to get around this issue? Any help would be greatly appreciated.

Thanks

Question: Why don’t you use the native function designed for this task?

http://php.net/array-filter

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.