I have JSON Response Request and want make one variable $Where from them, this the JSON pattern :
"filter":
{"filters":[
{"logic":"and","filters":[
{"field":"NAMA","operator":"eq","value":"Rahmat"},
{"field":"NAMA","operator":"eq","value":"Rosadi"}
]},{"logic":"or","filters":[
{"field":"NIPK","operator":"eq","value":"1919191919"},
{"field":"NIPK","operator":"eq","value":"818181818181"}
]},{"logic":"and","filters":[
{"field":"JK","operator":"eq","value":"P"},
{"field":"JK","operator":"eq","value":"L"}
]}
],
"logic":"and"}
Can anyone tell me how make thats patter to singgle row PHP variable like this :
$Where = "(NAMA = 'Rahmat' or NAMA = 'Rosadi') and (NIPK = '1919191919' or NIPK != '818181818181') and (JK='P' AND JK='L')"
Sorry for my bad english.
You can enumerate the
filterslist into PHP arrays, building each of them as a list of SQL OR values, then join them withimplode(' OR ', ...); and further implode the arrays in a list of ‘ AND ‘ clauses.This approach loses much of the flexibility allowed from your JSON setup, though; it can be extended to an
AND-list of eitherOR-clauses or single clauses, and maybe coalesceORclauses for the same field using theINsyntax, but not much more.You can look to algorithm that convert from RPN notation to algebraic to look for a more powerful (albeit more complex) approach.
The simple version is
Using
json_decode()an object can be retrieved that can be manipulated thus: