2014-09-05 119 views
-2

如何解析XML並獲取節點。解析XML並取消設置節點

+0

你嘗試過什麼?你的標籤很混亂 - node.js與事物有什麼關係? – Steve 2014-09-05 11:48:56

+0

試過json_decode()? – Nutic 2014-09-05 11:51:25

+0

使用解碼json和遞歸樹迭代器 – inf3rno 2014-09-05 11:54:16

回答

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; 
} 
+0

謝謝你的工作f9 – 2014-09-08 19:01:46