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));
}
Sounds pretty straight forward to me: you iterate over the input array and accept the elements only if the output does not yet contain such a candidate...
<?php
$input = json_decode(<<<EOT
[
{
"score": "value2",
"number": "1",
"finalScore": "-1"
}, {
"score": "value3",
"number": "2",
"finalScore": "5"
}, {
"score": "value4",
"number": "2",
"finalScore": "5"
}, {
"score": "value5",
"number": "3",
"finalScore": "-1"
}
]
EOT);
$output = [];
array_walk($input, function ($entry) use (&$output) {
if (!array_key_exists($entry->number, $output)) {
$output[$entry->number] = $entry;
}
});
print_r(array_values($output));
The output obviously is:
Array
(
[0] => stdClass Object
(
[score] => value2
[number] => 1
[finalScore] => -1
)
[1] => stdClass Object
(
[score] => value3
[number] => 2
[finalScore] => 5
)
[2] => stdClass Object
(
[score] => value5
[number] => 3
[finalScore] => -1
)
)
Simple approache:
json_decode
with second args true
.$scores_filtered
, second to keep track only unique numbers and $index
to keep the order ascending of $scores_filtered
array.$unique_numbers
) if So, store it in $unique_numbers
. get that score and store in $scores_filtered
array.$json = '[{
"score": "value2",
"number": "1",
"finalScore": "-1"
},{
"score": "value3",
"number": "2",
"finalScore": "5"
},{
"score": "value4",
"number": "2",
"finalScore": "5"
},{
"score": "value5",
"number": "3",
"finalScore": "-1"
}
]';
$scores = json_decode($json, true);
$scores_filtered = [];
$unique_numbers = [];
$index = 0;
for($i = 0; $i < count($scores); $i++) {
$score = $scores[$i];
if(!in_array($score['number'], $unique_numbers)){
$unique_numbers[] = $score['number'];
$scores_filtered[$index]["score"] = $score["score"];
$scores_filtered[$index]["number"] = $score["number"];
$scores_filtered[$index]["finalScore"] = $score["finalScore"];
$index += 1;
}
}
Output:
echo "<pre>";
print_r(json_encode($scores_filtered, JSON_PRETTY_PRINT));
/*
[
{
"score": "value2",
"number": "1",
"finalScore": "-1"
},
{
"score": "value3",
"number": "2",
"finalScore": "5"
},
{
"score": "value5",
"number": "3",
"finalScore": "-1"
}
]
*/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.