fork download
  1. <?php
  2.  
  3. // Your goal is to count how many items exist that have an age equal to or greater than 50, and print this final value.
  4.  
  5. $ch = curl_init('https://c...content-available-to-author-only...e.com/api/challenges/json/age-counting');
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  7. curl_setopt($ch, CURLOPT_HEADER, 0);
  8. $data = curl_exec($ch);
  9. curl_close($ch);
  10.  
  11. $json_data = json_decode($data, true);
  12. print_r($json_data);
  13. $items = explode(', ', $json_data['data']);
  14. $count = array_reduce($items, function ($count, $item) {
  15. if (strpos($item, 'age=') !== false) {
  16. $age = explode('=', $item)[1];
  17. if ($age >= 50) return $count + 1;
  18. }
  19. return $count;
  20. }, 0);
  21.  
  22. print_r($count); // 128
  23.  
  24. ?>
Success #stdin #stdout 0.04s 26284KB
stdin
Standard input is empty
stdout
Standard output is empty