php - how to match a key value pair in a JSON -
[ {"category":["30","32","33"],"type":"30 days","price":"100","id":"0"}, {"category":["34","37","47"],"type":"30 days","price":"200","id":1}, {"category":["46"],"type":"40 days","price":"100","id":2} ]
in above json, how category has type: 30days.
expected output as.
$categories = array{0=>30,1=>32,2=>33,3=>34,4=>37,4=>47}
you can use array_walk
as
$json = '[ {"category":["30","32","33"],"type":"30 days","price":"100","id":"0"}, {"category":["34","37","47"],"type":"30 days","price":"200","id":1}, {"category":["46"],"type":"40 days","price":"100","id":2} ]'; $arr = json_decode($json,true); $categories = array(); array_walk($arr,function($v,$k)use(&$categories,$arr){ if($v['type'] == "30 days"){ $categories = array_merge($categories,$v['category']); } });
Comments
Post a Comment