Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
900 views
in Technique[技术] by (71.8m points)

php - how to search for multiple key/value pairs in an array of associative arrays and return matching items?

This is the associative array:

 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 ()
        )
)

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

                )

        )

)

Ideal output should be first element from tableData as 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.

FYI- I am new to PHP.

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...