如何使其仅保留唯一值
<?php
header('Content-Type: application/json');
function super_unique($array,$key){
$temp_array = [];
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr = array();
$arr[0]['id'] = null;
$arr[0]['name'] = 'John';
$arr[1]['id'] = 12;
$arr[1]['name'] = 'John';
$arr[2]['id'] = null;
$arr[2]['name'] = null;
$arr[3]['id'] = 54;
$arr[3]['name'] = 'Ammie';
$arr[4]['id'] = 23;
$arr[4]['name'] = 'Martin';
$arr[5]['id'] = 54;
$arr[5]['name'] = null;
$arr[6]['id'] = 342;
$arr[6]['name'] = 'Anna';
$arr[7]['id'] = 64;
$arr[7]['name'] = 'Tom';
$arr[8]['id'] = 64;
$arr[8]['name'] = null;
$arr[9]['id'] = 364;
$arr[9]['name'] = null;
$arr[10]['id'] = null;
$arr[10]['name'] = 'Piter';
$arr = super_unique($arr,'id');
$arr = super_unique($arr,'name');
$arr = array_values($arr);
echo json_encode($arr);
?>
我想达到这个结果
[
{
"id": 12,
"name": "John"
},
{
"id": 54,
"name": "Ammie"
},
{
"id": 23,
"name": "Martin"
},
{
"id": 342,
"name": "Anna"
},
{
"id": 64,
"name": "Tom"
},
{
"id": 364,
"name": null
},
{
"id": null,
"name": 'Piter'
}
]