php - json decode and encode - iterate through multi dimensional array and remove key value if equals certain value -
basically json output -output php.
[{"attr":{"id":"node_2","rel":"default"},"data":"c:","state":"closed"},{"attr":{"id":"node_3","rel":"drive"},"data":"d:","state":"closed"}]
so because rel equal default
{"attr":{"id":"node_2","rel":"default"},"data":"c:","state":"closed"}
i need remove array.
i have thought of maybe using
foreach($arr $key => &$item) { if ($value['rel'] == 'default'{ unset($arr[$key]);
this however, wont work reason. have no idea if method best way, or whether there better way achieve this. need decode , encode it.
you can use json_encode , json_decode parse json jesse bunch said.
after decoded json posted, returned object. call on object have things bit diffrent.
$arr = json_decode('[{"attr":{"id":"node_2","rel":"default"},"data":"c:","state":"closed"},{"attr":{"id":"node_3","rel":"drive"},"data":"d:","state":"closed"}]'); foreach($arr $key => $row) { if ($row->attr->rel == 'default'){ unset($arr[$key]); } } var_dump($arr);
the $arr no longer contain default rel
Comments
Post a Comment