通过jquery或php删除包含值的json中的字符串

时间:2012-07-06 14:20:52

标签: php jquery mysql json

我有json字符串,由server.php

生成
[{"attr":{"id":"node_7","rel":"default"},"data":"doc.html","state":""},
 {"attr":{"id":"node_8","rel":"folder"},"data":"New node","state":"closed"},
 {"attr":{"id":"node_9","rel":"folder"},"data":"New node","state":""}]

如何删除包含值rel=default

的完整字符串

这是我为server.php。

编写的代码
require_once("config.php");
$jstree = new json_tree();




echo $jstree->{$_REQUEST["operation"]}($_REQUEST);
die();

2 个答案:

答案 0 :(得分:1)

使用PHP:

// convert json string to array
$json = json_decode($json_string);

// filter out items
$json = array_filter($json, function($item)
{
    return $item->attr->rel != "default";
});

// convert back to string
$json_string = json_encode($json);

答案 1 :(得分:0)

您可以使用data.items.splice(X,Y);删除元素;)