解析xml并取消设置节点

时间:2014-09-05 11:46:02

标签: xml

如何解析xml并获取节点。

1 个答案:

答案 0 :(得分:1)

您可以使用json_decode解码字符串,删除所有非活动元素的递归函数,最后json_encode将数组恢复为json字符串

$json = json_decode($data, true);
$json['categoryTree'] = filter_inactive($json['categoryTree']);
echo json_encode($json);

function filter_inactive($data) {
    $t = array();
    foreach ($data as $child) {
        if ($child['is_active'] == '1') {
            if (isset($child['children'])) {
                $child['children'] = filter_inactive($child['children']);
            }
            $t[] = $child;
        }
    }

    return $t;
}