我有一个大型多维数组,如下所示:
Array(
[1] => Array ( [type] => blah1 [category] => cat1 [exp_range] => this_week )
[2] => Array ( [type] => blah1 [category] => cat2 [exp_range] => next week )
[3] => Array ( [type] => blah1 [category] => cat1 [exp_range] => next week )
[4] => Array ( [type] => blah2 [category] => cat2 [exp_range] => this_week )
)
我希望能够用多个过滤器过滤这个数组。
例如。过滤 category = cat1 和 type = blah1 将返回数组 1 和 3。
我有以下函数会返回键 1、2、3,这是不正确的,因为数组 2 没有同时具有 cat1 和 blah1
任何人都可以看到我需要做什么才能使它正常工作吗?
还有可能将 sortin 合并到这个函数中吗?如果可以的话如何?
function array_searcher($needles, $array) {
foreach ($needles as $needle) {
foreach ($array as $key => $value) {
foreach ($value as $v) {
if ($v == $needle) {
$keys[] = $key;
}
}
}
}
return $keys;
}
最佳答案
我决定重写我的答案以适应过滤和排序。我采用了一种高度面向对象的方法来解决这个问题,我将在下面详细介绍。
您可以在这个 ideone.com live demonstration 查看所有这些代码的运行情况。 .
我做的第一件事是定义两个接口(interface)。
interface Filter {
public function filter($item);
}
interface Comparator {
public function compare($a, $b);
}
顾名思义,Filter
用于过滤,Comparator
用于比较。
接下来,我定义了三个实现这些接口(interface)的具体类,并完成了我想要的。
首先是KeyComparator
。此类只是将一个元素的键与另一个元素的键进行比较。
class KeyComparator implements Comparator {
protected $direction;
protected $transform;
protected $key;
public function __construct($key, $direction = SortDirection::Ascending, $transform = null) {
$this->key = $key;
$this->direction = $direction;
$this->transform = $transform;
}
public function compare($a, $b) {
$a = $a[$this->key];
$b = $b[$this->key];
if ($this->transform) {
$a = $this->transform($a);
$b = $this->transform($b);
}
return $a === $b ? 0 : (($a > $b ? 1 : -1) * $this->direction);
}
}
您可以指定排序方向,以及在比较每个元素之前要对其进行的转换。我定义了一个帮助类来封装我的 SortDirection
值。
class SortDirection {
const Ascending = 1;
const Descending = -1;
}
接下来,我定义了 MultipleKeyComparator
,它采用多个 KeyComparator
实例,并使用它们将两个数组相互比较。它们添加到 MultipleKeyComparator
的顺序是优先顺序。
class MultipleKeyComparator implements Comparator {
protected $keys;
public function __construct($keys) {
$this->keys = $keys;
}
public function compare($a, $b) {
$result = 0;
foreach ($this->keys as $comparator) {
if ($comparator instanceof KeyComparator) {
$result = $comparator->compare($a, $b);
if ($result !== 0) return $result;
}
}
return $result;
}
}
最后,我创建了 MultipleKeyValueFilter
,它用于根据键/值对数组过滤数组:
class MultipleKeyValueFilter implements Filter {
protected $kvPairs;
public function __construct($kvPairs) {
$this->kvPairs = $kvPairs;
}
public function filter($item) {
$result = true;
foreach ($this->kvPairs as $key => $value) {
if ($item[$key] !== $value)
$result &= false;
}
return $result;
}
}
现在,给定输入数组(请注意,我对它们进行了一些重新排列以使排序更加明显):
$array = array (
'1' => array ('type' => 'blah2', 'category' => 'cat2', 'exp_range' => 'this_week' ),
'2' => array ('type' => 'blah1', 'category' => 'cat1', 'exp_range' => 'this_week' ),
'3' => array ('type' => 'blah1', 'category' => 'cat2', 'exp_range' => 'next_week' ),
'4' => array ('type' => 'blah1', 'category' => 'cat1', 'exp_range' => 'next_week' )
);
可以通过执行以下操作来实现排序:
$comparator = new MultipleKeyComparator(array(
new KeyComparator('type'),
new KeyComparator('exp_range')
));
usort($array, array($comparator, 'compare'));
echo "Sorted by multiple fields\n";
print_r($array);
可以通过执行以下操作来实现过滤:
$filter = new MultipleKeyValueFilter(array(
'type' => 'blah1'
));
echo "Filtered by multiple fields\n";
print_r(array_filter($array, array($filter, 'filter')));
至此,我已经为您提供了大量代码。我建议您的下一步是将这两个部分组合成一个类。然后,这个单一的类将同时应用过滤和排序。
关于php - 过滤和排序具有多个值的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21319729/