php json decode忽略重复值

时间:2013-05-20 17:03:55

标签: php json

Php json解码,如何重入值重复?在我的情况下,如果$data->a$data->b重复,则仅打印第一个出现的值。

[
{"a":"1","b":"2","c":"content1"},
{"a":"1","b":"3","c":"content2"},//print
{"a":"1","b":"3","c":"content3"},//duplicate "a":"1","b":"3", do not print
{"a":"2","b":"1","c":"content4"},
{"a":"2","b":"2","c":"content5"},//print
{"a":"2","b":"2","c":"content6"},//duplicate "a":"2","b":"2", do not print
{"a":"2","b":"3","c":"content7"}
]

这不是一件容易的array_unique()可以工作,请求帮助,谢谢。

1 个答案:

答案 0 :(得分:3)

使用哈希数组!

$tmp=json_decode($input);
$data=array();
foreach ($tmp as $item) {
  $idx=$item->a."::".$item->b;
  if (!isset($data[$idx])) $data[$idx]=$item;
}
$data=array_values($data);