search in json and give the result as json for likely string values in php -
i have set json data. in have return json key , value pair in php. used json follows
[{ "id": "1995", "name": "banahatti" }, { "id": "5074", "name": "kolhapur(maharashtra)" }, { "id": "2356", "name": "bmbur" }, { "id": "906", "name": "ammla" }, { "id": "536", "name": "puttur" }, { "id": "1308", "name": "poogolam" }, { "id": "1217", "name": "sarangpur" }, { "id": "826", "name": "hiriyur" }, { "id": "24911", "name": "himmatnagar" }, { "id": "3993", "name": "podili" }]
in json values , have search , json ,if give name value starting b
means have result json follows
[{ "id": "1995", "name": "banahatti" }, { "id": "2356", "name": "bmbur" }]
how can able achieve in php.
php knows nothing json, convert array , use array_filter, , convert json. not super effecient, better trying regex. following:
$arr = json_decode($json, true); $res = array_filter($arr, function($val) { return substr($val['name'],0,1) == 'b'; }); $newjson = json_encode($res);
Comments
Post a Comment