问题描述
这是关联数组:
Array
(
[tableData] => Array
(
[0] => Array
(
[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] => Array
(
[0] => Array
(
[status] => 10
[manufacturer] => HP
[model] => HP BL460C GEN8
[hardware_color] => #0066b3
)
)
[full_name] => Valay Desai
[email_address] => [email protected]
)
[1] => Array
(
[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] => Array
(
[0] => Array
(
[status] => 10
[manufacturer] => SUN
[model] => SUN GEN8
[hardware_color] => #0066b3
)
)
[full_name] => Chako Desai
[email_address] => [email protected]
)
......
[500] => Array ()
)
)
我知道使用array_filter
过滤数据并比较值的正常方法.我想在tableData
中搜索以下键/值对.
I know normal way to filter out data by using array_filter
and compare the value. I would like to search for following key/value pairs in tableData
.
Array
(
[booking_name] => abc
[pdg] => Array
(
[0] => Array
(
[name] => Invalid
[value] => Invalid
)
[1] => Array
(
[name] => assure
[value] => assure
)
)
[user_area] => Array
(
[0] => Array
(
[name] => es st
[value] => es st
)
[1] => Array
(
[name] => Invalid
[value] => Invalid
)
[2] => Array
(
[name] => a&o
[value] => a&o
)
)
)
理想的输出应该是tableData
中的第一个元素,因为它具有 booking_name = abc,pdg = assure和user_area = es st
Ideal output should be first element from tableData
as it has booking_name=abc, pdg=assure and user_area=es st
我尝试过:
// 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);
});
这总是返回空白数组.
仅供参考-我是PHP
的新手.
FYI- I am new to PHP
.
更新1:
我已经使用下面的方法来获取具有visible:true
的对象.试过类似的问题,但无法获得理想的结果.
I've used below way to get objects who has 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);
});
如何在数组的关联数组上应用作为数组数组传递的多个过滤条件?
推荐答案
尝试此解决方案.
$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;
}
}
这篇关于PHP-如何在关联数组的数组中搜索多个键/值对并返回匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!