fork download
  1. <?php
  2. // Initialize the cURL session
  3. $ch = curl_init('https://c...content-available-to-author-only...e.com/api/challenges/json/age-counting');
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  5. curl_setopt($ch, CURLOPT_HEADER, 0);
  6. $data = curl_exec($ch);
  7.  
  8. // Decode the JSON response
  9. $data = json_decode($data, true);
  10.  
  11. // Split the 'data' string by commas to process each key-value pair
  12. $entries = explode(',', $data['data']);
  13.  
  14. // Initialize an array to store the age frequency
  15. $ageFrequency = [];
  16.  
  17. // Loop through each entry to extract the ages
  18. foreach ($entries as $entry) {
  19. // Split each entry by "=" to get the key-value pair
  20. list($key, $age) = explode('=', $entry);
  21.  
  22. // Only process entries where the key starts with "age"
  23. if (strpos($key, 'age') !== false) {
  24. $age = trim($age); // Remove any extra spaces
  25.  
  26. // Add the age to the frequency array or increment its count
  27. if (isset($ageFrequency[$age])) {
  28. $ageFrequency[$age]++;
  29. } else {
  30. $ageFrequency[$age] = 1;
  31. }
  32. }
  33. }
  34.  
  35. // Sort the ages by ascending order
  36. ksort($ageFrequency);
  37.  
  38. // Prepare the output as an array of objects
  39. $output = [];
  40. foreach ($ageFrequency as $age => $count) {
  41. $output[] = ['age' => (int)$age, 'count' => $count];
  42. }
  43.  
  44. // Print the result
  45. print_r($output);
  46. ?>
  47.  
Success #stdin #stdout #stderr 0.03s 26400KB
stdin
Standard input is empty
stdout
Array
(
)
stderr
PHP Notice:  Undefined offset: 1 in /home/jGxsWe/prog.php on line 21