簡體   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