过滤和放大器;排序多维数组包含多个值 [英] Filter & sort multidimensional array with multiple values
问题描述
我有一个大的多维数组有点像如下:
阵列(
[1] =>阵列([类型] = GT; blah1【类别】=> CAT1 [exp_range] => this_week)
[2] =>阵列([类型] = GT; blah1【类别】=> CAT2 [exp_range] =>接下来一周)
[3] =>阵列([类型] = GT; blah1【类别】=> CAT1 [exp_range] =>接下来一周)
[4] =>阵列([类型] = GT; blah2【类别】=> CAT2 [exp_range] => this_week)
)
我希望能够过滤这个数组包含多个过滤器。结果
例如。过滤那些类别= CAT1和类型= blah1将返回数组1和3。
我有下面的函数将返回键1,2,3这是不正确的数组2犯规兼得的 CAT1 和 blah1
有人能看到什么,我需要做的就是这个工作?
此外,才有可能纳入sortin这个功能,如果又如何?
函数array_searcher($针,$数组){
的foreach($针为$针){
的foreach($数组$关键=> $值){
的foreach($值V $){
如果($ V == $针){
$键[] = $键;
}
}
}
}
返回$键;
}
的我决定重写我的回答同时容纳过滤和排序。我花了大量面向对象的方法来解决这个问题,我将在下面详细。的
您可以看到这一切code的作用这个 ideone.com现场演示 。
我做的第一件事就是定义了两个接口。
接口过滤器{
公共职能过滤器($项目);
}接口比较{
公共职能比较($ A,$ B);
}
由于其名称所暗示的,过滤器
用于过滤和比较
用于比较。
接下来,我实现这些接口 定义了三个具体的类,完成我想要的东西。
首先是 KeyComparator
。这个类简单的一个元素的键比较另一个元素的关键。
类KeyComparator实现比较{
保护$方向;
保护$变换;
保护$关键; 公共职能__construct($键,$方向= SortDirection ::递增,$变换= NULL){
$这个 - >键= $键;
$这个 - >方向= $方向;
$这个 - >变换= $变换;
} 公共职能比较($ A,$ B){
$ a = $ a [$这个 - >键];
$ B = $ B [$这个 - >键]; 如果($这个 - >变换){
$ a = $这个 - >变换($ A);
$ B = $这个 - >变换($ B);
} 返回$一个=== $ B? 0:(($一个> $ b 1分配:-1)* $这 - >方向);
}
}
可以指定排序的方向,以及需做每个元素他们相比之前的变换。我定义了一个帮助类来封装我的 SortDirection
值。
类SortDirection {
常量递增= 1;
常量降序= -1;
}
接下来,我定义的 MultipleKeyComparator
这需要多个 KeyComparator
实例,并用它们两个数组相互比较。它们被添加到订单 MultipleKeyComparator
是precedence的顺序。
类MultipleKeyComparator实现比较{
保护$键; 公共职能__construct($键){
$这个 - >键= $键;
} 公共职能比较($ A,$ B){
$结果= 0; 的foreach($这个 - >密钥为$比较){
如果($比较的instanceof KeyComparator){
$结果= $比较器 - >比较($ A,$ B); 如果($结果== 0!)返回$结果;
}
} 返回$结果;
}
}
最后,我创建 MultipleKeyValueFilter
这意味着基于键/值对的数组来过滤数组:
类MultipleKeyValueFilter实现过滤器{
保护$ kvPairs; 公共职能__construct($ kvPairs){
$这个 - > kvPairs = $ kvPairs;
} 公共职能过滤器($项目){
$结果= TRUE; 的foreach($这个 - > kvPairs为$关键=> $值){
如果($项目[$关键]!== $值)
$结果&安培; = FALSE;
} 返回$结果;
}
}
现在,给定输入数组(的通知我重新安排他们一点使分拣明显的):
$阵列=阵列(
'1'=>阵列('类型'=>'blah2','分类'=>'CAT2','exp_range'=>'this_week'),
'2'= GT;阵列('类型'=>'blah1','分类'=>'CAT1','exp_range'=>'this_week'),
'3'= GT;阵列('类型'=>'blah1','分类'=>'CAT2','exp_range'=>'next_week'),
'4'= GT;阵列('类型'=>'blah1','分类'=>'CAT1','exp_range'=>'next_week')
);
排序可以通过执行以下操作来实现:
$ =比较新MultipleKeyComparator(阵列(
新KeyComparator(类型),
新KeyComparator('exp_range')
));usort($数组,数组($比较,比较));回声由多个领域的\\ n排序;
的print_r($数组);
过滤可通过以下操作来实现:
$ =过滤器新MultipleKeyValueFilter(阵列(
'型'=> blah1
));回声由多个领域的\\ n过滤;
的print_r(array_filter($数组,数组($过滤器,'过滤')));
在这一点上我已经给你code的很大。我建议你的下一步是将这些两块合并成一个类。这种单一类将同时适用于过滤和排序在一起。
I have a large multidimensional array something like the below:
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 )
)
I want to be able to filter this array with multiple filters.
eg. filtering where category = cat1 and type = blah1 would return array 1 and 3.
I have the below function that would return keys 1,2,3 which is incorrect as array 2 doesnt have both cat1 and blah1
Can anyone see what I need to do to get this working?
Also would it be possible to incorporate sortin in this function, if so how?
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;
}
I decided to rewrite my answer to accommodate both filtering and sorting. I took a heavily object oriented approach to solving this problem, which I will detail below.
You can see all of this code in action at this ideone.com live demonstration.
The first thing I did was define two interfaces.
interface Filter {
public function filter($item);
}
interface Comparator {
public function compare($a, $b);
}
As their names suggest, Filter
is used for filtering, and Comparator
is used for comparing.
Next, I defined three concrete classes that implements these interfaces, and accomplish what I wanted.
First is KeyComparator
. This class simply compares the key of one element to the key of another element.
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);
}
}
You can specify a sort direction, as well as a transformation to be done to each element before they are compared. I defined a helped class to encapsulate my SortDirection
values.
class SortDirection {
const Ascending = 1;
const Descending = -1;
}
Next, I defined MultipleKeyComparator
which takes multiple KeyComparator
instances, and uses them to compare two arrays against each other. The order in which they are added to the MultipleKeyComparator
is the order of precedence.
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;
}
}
Finally, I created MultipleKeyValueFilter
which is meant to filter an array based on an array of key/value pairs:
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;
}
}
Now, given the input array (Notice I rearranged them a bit to make the sorting obvious):
$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' )
);
Sorting can be achieved by doing the following:
$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);
Filtering can be achieved by doing the following:
$filter = new MultipleKeyValueFilter(array(
'type' => 'blah1'
));
echo "Filtered by multiple fields\n";
print_r(array_filter($array, array($filter, 'filter')));
At this point I've given you a great deal of code. I'd suggest that your next step is to combine these two pieces into a single class. This single class would then apply both filtering and sorting together.
这篇关于过滤和放大器;排序多维数组包含多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!