[{
"sys_id": "2015-07-018",
"account_id": "2015-07-018",
"Names": [{
"fname": "Jackie",
"mname": "Lee",
"lname": "Chan",
"suffix": "Jr",
"city": "Tokyo",
"town": "Shinagawa-ku",
"dstrct": "District 2",
"street": "Jr",
"contactnum": "1234"
}, {
"fname": "Jackie",
"mname": "Lee",
"lname": "Chan",
"suffix": "Jr",
"city": "Tokyo",
"town": "Shinagawa-ku",
"dstrct": "District 2",
"street": "Jr",
"contactnum": "1234"
}, {
"fname": "Jackie",
"mname": "Lee",
"lname": "Chan",
"suffix": "Jr",
"city": "Tokyo",
"town": "Shinagawa-ku",
"dstrct": "District 2",
"street": "Jr",
"contactnum": "1234"
}, {
"fname": "Jackie",
"mname": "Lee",
"lname": "Chan",
"suffix": "Sr",
"city": "Tokyo",
"town": "Shinagawa-ku",
"dstrct": "District 2",
"street": "Sr",
"contactnum": "1234"
}, {
"fname": "Jackie",
"mname": "Lee",
"lname": "Chan",
"suffix": "Sr",
"city": "Tokyo",
"town": "Shinagawa-ku",
"dstrct": "District 2",
"street": "Sr",
"contactnum": "1234"
}, {
"fname": "Jackie",
"mname": "Lee",
"lname": "Chan",
"suffix": "Sr",
"city": "Tokyo",
"town": "Shinagawa-ku",
"dstrct": "District 2",
"street": "Sr",
"contactnum": "1234"
}]
}]
这是我从ajax请求的结果,我想从服务器端或jquery端在我的服务器端删除重复的结果,我已经尝试了删除重复数组的最常见的方式,如
$input = array_map("unserialize", array_unique(array_map("serialize", $new_data)));
$new_data1 = array_values($input);
echo json_encode($new_data1, JSON_UNESCAPED_UNICODE);
其中$new_data
是我的选择查询的结果。print_r($new_data)
将导致
Array(
[2015 - 07 - 018] => Array(
[sys_id] => 2015 - 07 - 018[account_id] => 2015 - 07 - 018[Names] => Array(
[0] => Array(
[fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Jr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Jr[contactnum] => 1234)[1] => Array(
[fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Jr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Jr[contactnum] => 1234)[2] => Array(
[fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Jr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Jr[contactnum] => 1234)[3] => Array(
[fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Sr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Sr[contactnum] => 1234)[4] => Array(
[fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Sr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Sr[contactnum] => 1234)[5] => Array(
[fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Sr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Sr[contactnum] => 1234))))
Update
我尝试了print_r($new_data1);
,结果与print_r($new_data);
的结果完全相同
寻找一种方法来删除重复,我发现这个
function super_unique($array) {
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach($result as $key => $value) {
if (is_array($value)) {
$result[$key] = super_unique($value);
}
}
return $result;
}
Array ( [1] => Array ( [sis_id] => 2015-07-018 [account_id] => 2015-07-018 [Names] => Array ( [0] => Array ( [fname] => Jackie [mname] => Lee [lname] => Chan [suffix] => Jr [city] => Tokyo [town] => Shinagawa-ku [brgy] => District 2 [contactnum] => 1234 ) [3] => Array ( [fname] => Jackie [mname] => Lee [lname] => Chan [suffix] => Sr [city] => Tokyo [town] => Shinagawa-ku [brgy] => District 2 [contactnum] => 1234 ) ) ) )
我想要旧格式的输出
如果您将数组中的每个对象序列化为字符串(在我看来json是最简单的),然后使用本机函数删除重复项,然后反序列化回对象会怎么样?
Sudo代码看起来像这样:
$serialized_array = [];
foreach($new_data as $data) {
$serialized_array[] = json_encode($data);
}
$new_data = []; //reset new_data
$unique = array_unique($serialized_array);
foreach($serialized_array as $string) {
$new_data[] = json_decode($string);
}
//new_data should now only contain unique data
你所做的和这之间的主要区别是序列化和反序列化每个对象单独而不是整个数组。