doupu3635 2015-06-28 00:50
浏览 64

来自JSON的PHP array_filter动态过滤器

I got the two following arrays as input data:

$filters = 
  [
    {
      "Key": "ao",
      "Value": "5",
      "FilterComperator": ">=",
      "FilterOperator": " && "
    },
    {
      "Key": "name",
      "Value": "Joe",
      "FilterComperator": "<>",
      "FilterOperator": " && "
    },
    {
      "Key": "ao",
      "Value": "10",
      "FilterComperator": "<=",
      "FilterOperator": " && "
    }
  ]


$arr = [
    {
      "id":1,
      "ao": 13
    },
    {
      "id":2,
      "ao": 10
    },
    {
      "id":3,
      "ao": 6
    }
]

What I am trying to achieve is use the filters from $filters array so I can filter $arr without using php eval.

return array_filter($arr, function($k){
   return $k->ao >= '5' && $k->name <> 'Joe' && $k->ao <= '10';
});

Is there any suggestion? Perhaps I could use create_function() instead or anything else that could do the job.

Desired Output is an array using the filter criteria like this:

 $output = [
        {
          "id":2,
          "ao": 10
        },
        {
          "id":3,
          "ao": 6
        }
    ]
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
    • ¥15 cs loadimage运行不了,easyx也下了,没有用
    • ¥15 r包runway详细安装教程
    • ¥15 Html中读取Json文件中数据并制作表格
    • ¥15 谁有RH342练习环境
    • ¥15 STM32F407 DMA中断问题
    • ¥15 uniapp连接阿里云无法发布消息和订阅
    • ¥25 麦当劳点餐系统代码纠错
    • ¥15 轮班监督委员会问题。
    • ¥20 关于变压器的具体案例分析