dpl74687 2017-06-23 12:13
浏览 74

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

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

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 ?

  • 写回答

1条回答 默认 最新

  • duanbinmi8970 2017-06-23 15:03
    关注

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

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了