0

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()事,寻求帮助,谢谢。

4

1 回答 1

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);
于 2013-05-20T17:08:12.443 回答