简体   繁体   English

基于逻辑表达式在PHP中过滤数组

[英]Filter array in PHP based on logical expressions

I'm trying to implement a filter function in PHP. 我正在尝试在PHP中实现过滤功能。 My two-dimensional input array looks like the following: 我的二维输入数组如下所示:

name(string)    age(integer)    reg_date(date)
----------------------------------------------
John            22              12/03/2015
Anne            23              14/04/2015
Marry           24              01/02/2015
Tim             55              13/03/2015

What I want is a filter that can take a logical expression and apply it to the input source like the following (own draft): 我想要的是一个过滤器,它可以采用逻辑表达式并将其应用于输入源,如下所示(自己的草稿):

{name:'age' type:'integer', operator:'==', value:'23'} OR {name:'age' type:'integer', operator:'==', value:'24'} AND {name:'reg_date' type:'date', operator:'>', value:'12/03/2015', format:'d/m/Y'}

Expressed in words: Give me all entries where variable age is 23 or 24 and variable reg_date is later than 12/03/2015 用文字表达:给我所有变量年龄为23或24且变量reg_date晚于12/03/2015的条目

For each datatype (type), a filter should exist that should be able to process datatype dependent operations (for example, a date-filter should use the format field to parse the date in the correct format). 对于每种数据类型(类型),应该存在一个应该能够处理数据类型相关操作的过滤器(例如,日期过滤器应该使用format字段以正确的格式解析日期)。 It should easily be possible to add new datatypes. 应该可以轻松添加新的数据类型。

The correct output for the expression above would be 上面表达式的正确输出将是

name(string)    age(integer)    reg_date(date)
----------------------------------------------
Anne            23              14/04/2015

My question: Do I have to write my own lexer to parse the logical expression (which could become rather complex) and apply a filter I've written on my own or do filter functions I need already exist? 我的问题:我是否必须编写自己的词法分析器来解析逻辑表达式(这可能变得相当复杂)并应用我自己编写的过滤器或者我需要的过滤函数? I tried to parse my array into an XML structure and use XPath, unfortunately XPath 1.0 which is used by PHP cannot handle date formats. 我试图将我的数组解析为XML结构并使用XPath,遗憾的是PHP使用的XPath 1.0无法处理日期格式。 I took a look at filter_var in the PHP Filter class, but it looks like logical expressions cannot be used there. 我看了一下PHP Filter类中的filter_var,但看起来逻辑表达式不能在那里使用。

尝试使用array_walk()php函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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