let's suppose that the json response file has the following values:
$response = '{"count":2948,"errors":"","offers":[{"id":"1","name":"a"},{"id":"2","name":"b"},{"id":"3","name":"c"},{"id":"4","name":"a"},{"id":"5","name":"c"},{"id":"4","name":"a"},{"id":"4","name":"a"},{"id":"4","name":"b"}]}';
decode them:
$json = json_decode($response, true);
then remove the repeated offers:
// make sure that the required index is exists
if(!empty($json['offers'])){
$json = scan_json_array($json['offers']);
}
by the following recursive function:
function scan_json_array(array $arr, $index = 0){
// if we reached the last element of the array, exit!
if($index == (sizeof($arr)-1)){
return $arr;
}
for(; $index<sizeof($arr);){
$current = $arr[$index];
for($j=$index+1; $j<sizeof($arr); $j++){
$next = $arr[$j];
if($current['name'] === $next['name']){
// remove the matched element
unset($arr[$j]);
// re-index the array
$arr = array_values($arr);
// if it was the last element, increment $index to move forward to the next array element
if($j == (sizeof($arr)-1)){
$index++;
}
return scan_json_array($arr, $index);
}
}
$index++;
}
}