fork download
  1. <?php
  2.  
  3. $json = '{
  4. "result":[
  5. {
  6. "type":"Residence",
  7. "street":"Piazza di Spagna",
  8. "city":"-4:0"
  9. },
  10. {
  11. "type":"Residence",
  12. "street":"test",
  13. "city":"-4:1"
  14. }
  15. ]
  16. }';
  17.  
  18. header('Content-Type: text/plain');
  19.  
  20. try {
  21. $nodes = getJsonNodes($json);
  22. } catch (\InvalidArgumentException $e) {
  23. echo $e->getMessage();
  24. }
  25.  
  26. print_r($nodes);
  27.  
  28.  
  29. function getJsonNodes($json) {
  30. $nodes = array();
  31. $decoded = json_decode($json, true);
  32.  
  33. if (json_last_error() !== JSON_ERROR_NONE) {
  34. throw new \InvalidArgumentException('Invalid JSON String passed to getJsonNodes()');
  35. }
  36.  
  37. $result = $decoded['result'];
  38. if (is_array($result)) {
  39. $nodes = array_keys($result[0]);
  40. }
  41.  
  42. return $nodes;
  43. }
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Array
(
    [0] => type
    [1] => street
    [2] => city
)