result - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of result.php

  1. <?php
  2.  
  3. $a = json_decode('[{"id":"2","name":"Canada"},{"id":"19","name":"Bahrain"},{"id":"36","name":"Bulgaria"},{"id":"53","name":"Congo, Republic of the"},{"id":"70","name":"Eritrea"},{"id":"87","name":"Greenland"},{"id":"104","name":"Iran"},{"id":"121","name":"Lebanon"},{"id":"138","name":"Mauritania"},{"id":"155","name":"New Zealand"}]');
  4.  
  5. $b = json_decode('[
  6. {"id":"2","cn":"CA"},
  7. {"id":"7","cn":"AS"},
  8. {"id":"12","cn":"AG"},
  9. {"id":"17","cn":"AT"},
  10. {"id":"22","cn":"BY"},
  11. {"id":"27","cn":"BT"},
  12. {"id":"32","cn":"BR"},
  13. {"id":"37","cn":"BF"},
  14. {"id":"42","cn":"CV"},
  15. {"id":"47","cn":"CN"},
  16. {"id":"52","cn":"CD"},
  17. {"id":"57","cn":"HR"},
  18. {"id":"62","cn":"DJ"},
  19. {"id":"67","cn":"EG"},
  20. {"id":"72","cn":"ET"},
  21. {"id":"77","cn":"FR"},
  22. {"id":"82","cn":"GE"},
  23. {"id":"87","cn":"GL"},
  24. {"id":"92","cn":"GN"},
  25. {"id":"97","cn":"VA"}
  26. ]
  27. ');
  28.  
  29. // написать функцию, которая покажет id, name из массива $a, 
  30. // для тех id, которые отсутсвуют в массиве $b
  31.  
  32. echo showInfo($a, $b);
  33.  
  34. function showInfo(array $a, array $b) : string {
  35.     $get_id = function($elem) {
  36.         return $elem->id;
  37.     };
  38.     $b_ids = array_map($get_id, $b);
  39.     $b_id_flip = array_flip($b_ids);
  40.     
  41.     $res = '';
  42.     foreach ($a as $elem) {
  43.         if (!isset($b_id_flip[$elem->id])) {
  44.             $res .= $elem->id . ', ' . $elem->name . PHP_EOL;
  45.         }
  46.     }
  47.     
  48.     return $res;
  49. }
File Description
  • result
  • PHP Code
  • 28 Sep-2023
  • 1.38 Kb
You can Share it: