<?php
/**
* Removes duplicate HTTP responses.
*
* This function takes an array of HTTP response objects and returns a new array
* with duplicates removed. It's designed for development use and doesn't
* rely on external libraries. It compares responses based on their content.
*
* @param array $responses An array of HTTP response objects. Each object
* should have a 'body' property containing the response content.
* @return array A new array with duplicate HTTP responses removed.
*/
function removeDuplicateResponses(array $responses): array
{
$uniqueResponses = [];
$seenBodies = []; // Keep track of response bodies we've already encountered.
foreach ($responses as $response) {
// Check if the response body has been seen before.
if (!in_array($response['body'], $seenBodies)) {
$uniqueResponses[] = $response; // Add the response to the unique array.
$seenBodies[] = $response['body']; // Add the response body to the seen array.
}
}
return $uniqueResponses;
}
// Example Usage (for testing)
/*
class HttpResponse {
public $status_code;
public $body;
public function __construct($status_code, $body) {
$this->status_code = $status_code;
$this->body = $body;
}
}
$response1 = new HttpResponse(200, 'This is the first response.');
$response2 = new HttpResponse(200, 'This is the first response.');
$response3 = new HttpResponse(302, 'This is a different response.');
$response4 = new HttpResponse(200, 'This is the first response.'); // Duplicate
$responses = [$response1, $response2, $response3, $response4];
$uniqueResponses = removeDuplicateResponses($responses);
foreach ($uniqueResponses as $response) {
echo "Status Code: " . $response->status_code . "\n";
echo "Body: " . $response->body . "\n";
echo "\n";
}
*/
?>
Add your comment