<?php
// Initialize the cURL session
$ch = curl_init('https://c...content-available-to-author-only...e.com/api/challenges/json/age-counting');
// Decode the JSON response
// Split the 'data' string by commas to process each key-value pair
$entries = explode(',', $data['data']);
// Initialize an array to store the age frequency
$ageFrequency = [];
// Loop through each entry to extract the ages
foreach ($entries as $entry) {
// Split each entry by "=" to get the key-value pair
// Only process entries where the key starts with "age"
if (strpos($key, 'age') !== false) { $age = trim($age); // Remove any extra spaces
// Add the age to the frequency array or increment its count
if (isset($ageFrequency[$age])) { $ageFrequency[$age]++;
} else {
$ageFrequency[$age] = 1;
}
}
}
// Sort the ages by ascending order
// Prepare the output as an array of objects
$output = [];
foreach ($ageFrequency as $age => $count) {
$output[] = ['age' => (int)$age, 'count' => $count];
}
// Print the result
?>
PD9waHAKLy8gSW5pdGlhbGl6ZSB0aGUgY1VSTCBzZXNzaW9uCiRjaCA9IGN1cmxfaW5pdCgnaHR0cHM6Ly9jLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5lLmNvbS9hcGkvY2hhbGxlbmdlcy9qc29uL2FnZS1jb3VudGluZycpOwpjdXJsX3NldG9wdCgkY2gsIENVUkxPUFRfUkVUVVJOVFJBTlNGRVIsIHRydWUpOwpjdXJsX3NldG9wdCgkY2gsIENVUkxPUFRfSEVBREVSLCAwKTsKJGRhdGEgPSBjdXJsX2V4ZWMoJGNoKTsKY3VybF9jbG9zZSgkY2gpOwoKLy8gRGVjb2RlIHRoZSBKU09OIHJlc3BvbnNlCiRkYXRhID0ganNvbl9kZWNvZGUoJGRhdGEsIHRydWUpOwoKLy8gU3BsaXQgdGhlICdkYXRhJyBzdHJpbmcgYnkgY29tbWFzIHRvIHByb2Nlc3MgZWFjaCBrZXktdmFsdWUgcGFpcgokZW50cmllcyA9IGV4cGxvZGUoJywnLCAkZGF0YVsnZGF0YSddKTsKCi8vIEluaXRpYWxpemUgYW4gYXJyYXkgdG8gc3RvcmUgdGhlIGFnZSBmcmVxdWVuY3kKJGF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