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