samedi 27 juin 2015

php array_filter dynamic filters from json

I got the two following arrays as input data:

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

$arr = [ { "ao": 13 }, { "ao": 10 }, { "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.

Aucun commentaire:

Enregistrer un commentaire