简体   繁体   中英

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 .

Ideal output should be the first element from tableData because it has booking_name=abc , pdg=assure and 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:

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

How do I apply multiple filtering conditions passed as an array of arrays on an associative array of 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. 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. 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.

I am nesting the output inside of a tableData although I don't know if that's the exact output you desire. It is simple enough to unwrap the filtered array from the element.

Code: ( Demo )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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