I have the following associative array of objects:
[
0: {
"score": "value2",
"number": "1",
"finalScore": "-1"
},
1: {
"score": "value3",
"number": "2",
"finalScore": "5"
},
2: {
"score": "value4",
"number": "2",
"finalScore": "5"
},
3: {
"score": "value5",
"number": "3",
"finalScore": "-1"
}
]
Please, have in mind the following format is the prettified JSON string on the browser, after returning it from PHP through an echo json_encode($result)
I need to filter it based on the number
property value, in order to remove all duplicates with the same value for the number
property except the first one. This means that if two or more objects share the same value for number
, only the first one should remain.
Given this explanation, the filtered array from the example above would result on:
[
0: {
"score": "value2",
"number": "1",
"finalScore": "-1"
},
1: {
"score": "value3",
"number": "2",
"finalScore": "5"
},
2: {
"score": "value5",
"number": "3",
"finalScore": "-1"
}
]
I have made several attempts, the closest I've been is this funcion:
function array_iunique($array) {
$lowered = array_map('strtolower', $array);
return array_intersect_key($array, array_unique($lowered));
}