繁体   English   中英

使用来自另一个多维数组的多个规则过滤多维数组

[英]Filter multidimensional array using multiple rules from another multidimensional 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']
    ]
];

我知道array_filter可用于比较值,但我不确定如何对tableData中的数据执行多规则过滤。

理想的 output 应该是 tableData 的第一个元素,因为它有tableData booking_name=abc , pdg pdg=assureuser_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);
});

这总是返回空白数组。

更新1:

我使用以下方法来获取具有visible:true的对象。 对所提出的问题进行了类似的尝试,但无法获得理想的结果。

$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);
});

如何在 arrays 的关联数组上应用作为 arrays 数组传递的多个过滤条件?

试试这个解决方案。

$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;
 }
}

我认为我不会费心尝试为这项任务争吵 array_intersect array_intersect() -family 函数。 这些函数在迭代时执行排序,这在返回通过/失败类型评估时并不理想。

我会简化过滤数组的结构,使处理更直接。 一旦user_area pdg被展平到它们最有意义的部分,只需调用array_filter()并将三个评估写在一个返回中。 这将享受“短路”的性能优势,因此一旦遇到false结果,就会返回false ,而无需进行不必要的额外评估。

我将 output 嵌套在tableData内,尽管我不知道这是否正是您想要的 output。 从元素中解开过滤后的数组非常简单。

代码:(演示

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM