php - 如何在关联数组数组中搜索多个键/值对并返回匹配项?

时间:2017-06-23 12:13:38

标签: php arrays

这是关联数组:

 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] => valay@xyz.com
            )
            [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] => chako@xyz.com
            )
            ......
            [500] => Array ()
        )
)

我知道使用array_filter过滤掉数据的正常方法并比较该值。我想在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

我尝试过:

// 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的新手。

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

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

1 个答案:

答案 0 :(得分:1)

尝试此解决方案。

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