[英]Filter multidimensional array using multiple rules from another multidimensional array
I have a multidimensional input array and another multidimensional array containing rules by which to filter the input array.我有一个多维输入数组和另一个多维数组,其中包含过滤输入数组的规则。
$array = [
'tableData' => [
[
'booking_name' => 'abc/xyz/123',
'pdg' => 'assure',
'user_area' => 'es st',
'release' => 'oss72',
'start_date' => '2017-06-20 00:00:00',
'end_date' => '2017-06-23 00:00:00',
'asset_info' => [
[
'status' => 10,
'manufacturer' => 'HP',
'model' => 'HP BL460C GEN8',
'hardware_color' => ''#0066b3'
]
],
'full_name' => 'Valay Desai',
'email_address' => 'valay@xyz.com'
],
[
'booking_name' => 'abc/xyz/123',
'pdg' => 'assure',
'user_area' => 'ls reca',
'release' => 'oss72',
'start_date' => '2017-06-20 00:00:00',
'end_date' => '2017-06-23 00:00:00',
'asset_info' => [
[
'status' => 10,
'manufacturer' => 'SUN',
'model' => 'SUN GEN8',
'hardware_color' => '#0066b3'
]
],
'full_name' => 'Chako Desai',
'email_address' => 'chako@xyz.com'
]
]
];
$filterBy = [
'booking_name' => 'abc',
'pdg' => [
['name' => 'Invalid', 'value' => 'Invalid'],
['name' => 'assure', 'value' => 'assure']
],
'user_area' => [
['name' => 'es st', 'value' => 'es st'],
['name' => 'Invalid', 'value' => 'Invalid'],
['name' => 'a&o', 'value' => 'a&o']
]
];
I understand that array_filter
can be used to compare the values, but I'm not sure how to perform the multi-rule filtration on the data within tableData
.我知道array_filter
可用于比较值,但我不确定如何对tableData
中的数据执行多规则过滤。
Ideal output should be the first element from tableData
because it has booking_name=abc
, pdg=assure
and user_area=es st
.理想的 output 应该是 tableData 的第一个元素,因为它有tableData
booking_name=abc
, pdg pdg=assure
和user_area=es st
。
I tried with:我试过:
// bigarray is an originial array to be filtered
// filterObj is an array with multiple filter conditions
array_filter($bigarray, function ($val_array) use ($filterObj) {
$intersection = array_intersect_assoc($val_array, $filterObj);
return (count($intersection)) === count($filterObj);
});
This always returns blank array.这总是返回空白数组。
Update 1:更新1:
I've used below way to get objects who has visible:true
.我使用以下方法来获取具有visible:true
的对象。 Tried similar for the asked question but couldn't able to get the ideal result.对所提出的问题进行了类似的尝试,但无法获得理想的结果。
$columnVisible = array(
'visible' => 1,
);
$visibleColumns = array_filter($passedColumns, function ($val_array) use ($columnVisible) {
$intersection = array_intersect_assoc($val_array, $columnVisible);
return (count($intersection)) === count($columnVisible);
});
How do I apply multiple filtering conditions passed as an array of arrays on an associative array of arrays?如何在 arrays 的关联数组上应用作为 arrays 数组传递的多个过滤条件?
Try this solution. 试试这个解决方案。
$filters = array('pdg'=>array('xyzabc'), 'user_area'=>array('ls reca'));
$filter_items = array();
foreach( $items['tableData'] as $item ){
$i=0;
$is_match = true;
foreach( $filters as $key=>$value){
//$is_match = true;
if( !in_array( $item[$key], $value) ){
$is_match = false;
break;
}
//$is_match = true;
}
if( $is_match ){
$filter_items[] = $item;
}
}
I don't think I would bother trying to wrangle array_intersect()
-family functions for this task.我认为我不会费心尝试为这项任务争吵 array_intersect array_intersect()
-family 函数。 These functions perform sorting while they iterate and this is not going to be ideal while returing pass/fail type evaluations.这些函数在迭代时执行排序,这在返回通过/失败类型评估时并不理想。
I would simplify the filtering array's structure to make the processing more direct.我会简化过滤数组的结构,使处理更直接。 Once the pdg
and user_area
rules are flattened to their most meaningful parts, just call array_filter()
and write the three evaluations in a single return.一旦user_area
pdg
被展平到它们最有意义的部分,只需调用array_filter()
并将三个评估写在一个返回中。 This will enjoy the performance benefit of "short circuiting" so as soon as a false
outcome is encountered, the false
will be returned without making needless extra evaluations.这将享受“短路”的性能优势,因此一旦遇到false
结果,就会返回false
,而无需进行不必要的额外评估。
I am nesting the output inside of a tableData
although I don't know if that's the exact output you desire.我将 output 嵌套在tableData
内,尽管我不知道这是否正是您想要的 output。 It is simple enough to unwrap the filtered array from the element.从元素中解开过滤后的数组非常简单。
$filterBy['pdg'] = array_column($filterBy['pdg'], 'value');
$filterBy['user_area'] = array_column($filterBy['user_area'], 'value');
var_export(
[
'tableData' =>
array_filter(
$array['tableData'],
function($row) use($filterBy) {
return str_contains($row['booking_name'], $filterBy['booking_name'])
&& in_array($row['pdg'], $filterBy['pdg'])
&& in_array($row['user_area'], $filterBy['user_area']);
}
)
]
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.