从嵌套的JSON中删除重复项

时间:2015-04-07 22:28:51

标签: php arrays json

如何使用PHP从JSON中删除副本?

{
  "keyword": [{
    "consultion": [{
      "title": "\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636",
      "content": "\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f",
      "id": "1"
    }]
  }, {
    "consultion": [{
      "title": " \u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633",
      "content": " \u0645\u0627 \"\u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633\" \u0627\u0644\u062a\u064a \u0642\u062f \u064a\u0639\u0627\u0646\u064a \u0645\u0646\u0647\u0627 \u0645\u0631\u064a\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f",
      "id": "2"
    }]
  }, {
    "consultion": [{
      "title": "\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636",
      "content": "\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f",
      "id": "1"
    }]
  }]
}

3 个答案:

答案 0 :(得分:0)

<?php
    $data = json_decode('{"keyword":[{"consultion":[{"title":"\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636","content":"\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"1"}]},{"consultion":[{"title":" \u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633","content":" \u0645\u0627 \"\u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633\" \u0627\u0644\u062a\u064a \u0642\u062f \u064a\u0639\u0627\u0646\u064a \u0645\u0646\u0647\u0627 \u0645\u0631\u064a\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"2"}]},{"consultion":[{"title":"\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636","content":"\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"1"}]}]}');
    $data = $data->keyword[0];

    $new_data['title'] = $data->title;
    $new_data['consultion'] = $data->consultion;
    echo json_encode($new_data);
?>

PS:

<?php

    $data = json_decode('{"keyword":[{"consultion":[{"title":"\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636","content":"\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"1"}]},{"consultion":[{"title":" \u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633","content":" \u0645\u0627 \"\u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633\" \u0627\u0644\u062a\u064a \u0642\u062f \u064a\u0639\u0627\u0646\u064a \u0645\u0646\u0647\u0627 \u0645\u0631\u064a\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"2"}]},{"consultion":[{"title":"\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636","content":"\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"1"}]}]}');
    $keywords = $data->keyword;

    $new_data = array();

    foreach ($keywords as $consultion) {

        if (array_search($consultion, $new_data) === FALSE) {
            $new_data[] = $consultion;
        }
    }

    print_r($new_data);
    //echo json_encode($new_data);

    /******************** Example **********************************/
    echo "\r\n\r\n";
    $data = array();
    $data['consultion'][] = array('title' => 'a', 'id' => 1);
    $data['consultion'][] = array('title' => 'b', 'id' => 2);
    $data['consultion'][] = array('title' => 'a', 'id' => 1); //duplicated

    $encoded = json_encode($data);
    $data = json_decode($encoded);


    $new_data = array();

    foreach ($data->consultion as $obj) {

        if (array_search($obj, $new_data) === FALSE) {
            $new_data[] = $obj;
        }
    }

    print_r($new_data);
    //echo json_encode($new_data);

?>

答案 1 :(得分:0)

// copy them into anther array with check if content is taken before or not 

$array = json_decode(json, TRUE);
// [["title"=>"", "content"=>""],["title"=>"", "content"=>""]]
$arr = json_decode('{"keyword":[{"consultion":[{"title":"\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636","content":"\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"1"}]},{"consultion":[{"title":" \u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633","content":" \u0645\u0627 \"\u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633\" \u0627\u0644\u062a\u064a \u0642\u062f \u064a\u0639\u0627\u0646\u064a \u0645\u0646\u0647\u0627 \u0645\u0631\u064a\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"2"}]},{"consultion":[{"title":"\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636","content":"\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"1"}]}]} ',true);
$tmp = array();
foreach ($arr["keyword"] as $item){
   if(in_array( $item, $tmp  )  ){ continue; }
   $tmp[] = $item;
}

print_r($tmp);
 // or 
json_encode($tmp);

答案 2 :(得分:0)

只需解码JSON字符串,在关键字数组上运行array_unique并编码回JSON:

// Decode JSON into an array
$str = '{"keyword":[{"consultion":[{"title":"\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636","content":"\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"1"}]},{"consultion":[{"title":" \u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633","content":" \u0645\u0627 \"\u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633\" \u0627\u0644\u062a\u064a \u0642\u062f \u064a\u0639\u0627\u0646\u064a \u0645\u0646\u0647\u0627 \u0645\u0631\u064a\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"2"}]},{"consultion":[{"title":"\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636","content":"\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f","id":"1"}]}]}';
$array = json_decode($str, true);

// Remove duplicates from 'keyword' array
$array['keyword'] = array_unique($array['keyword'], SORT_REGULAR);

// Encode back into JSON
$json = json_encode($array, JSON_PRETTY_PRINT);
echo $json;

输出:

{
    "keyword": [
        {
            "consultion": [
                {
                    "title": "\u0645\u062f\u0629 \u0627\u0644\u0645\u0631\u0636",
                    "content": "\u0645\u0627 \u0645\u0639\u062f\u0644 \u0627\u0644\u0628\u0642\u0627\u0621 \u0639\u0644\u0649 \u0642\u064a\u062f \u0627\u0644\u062d\u064a\u0627\u0629 \u0628\u0639\u062f \u0627\u0644\u0625\u0635\u0627\u0628\u0629 \u0628\u0645\u0631\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f",
                    "id": "1"
                }
            ]
        },
        {
            "consultion": [
                {
                    "title": " \u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633",
                    "content": " \u0645\u0627 \"\u0645\u062a\u0644\u0627\u0632\u0645\u0629 \u063a\u0631\u0648\u0628 \u0627\u0644\u0634\u0645\u0633\" \u0627\u0644\u062a\u064a \u0642\u062f \u064a\u0639\u0627\u0646\u064a \u0645\u0646\u0647\u0627 \u0645\u0631\u064a\u0636 \u0627\u0644\u0623\u0644\u0632\u0647\u0627\u064a\u0645\u0631\u061f",
                    "id": "2"
                }
            ]
        }
    ]
}