3v4l.org

run code in 500+ PHP versions simultaneously
<?php $json = ' { "type": "FeatureCollection", "features": [{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [ -4.702540, 51.672791 ] }, "properties": { "Id": "1603", "Name": "TENBY", "Country": "Channel Islands", "ContinuousHeightsAvailable": true } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ -1.878530, 50.721680 ] }, "properties": { "Id": "1603A", "Name": "Bournmouth", "Country": "Channel Islands", "ContinuousHeightsAvailable": true } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ -2.35, 49.433333 ] }, "properties": { "Id": "1603A", "Name": "Maseline Pier", "Country": "Channel Islands", "ContinuousHeightsAvailable": true } }] }'; $locations = json_decode($json, true); #print_r($locations); $base_location = array( 'lat' => "51.672791", 'lng' => "-4.702540" ); $distances = array(); for ($c = 0; $c < count($locations["features"]); $c++) { echo "I = " .$c; } for ($c = 0; $c < count($locations["features"]); $c++) { foreach($locations as $i){ foreach ($i as $z) { foreach ($z['geometry'] as $key => $location) { if ($key == "coordinates"){ # print_r($z); $a = $base_location['lat'] - $location[1]; $b = $base_location['lng'] - $location[0]; $distance = sqrt(($a**2) + ($b**2)); $distances[$c] = $distance; echo "The value of key '$key' is '$location'", PHP_EOL; }} }} } print_r($base_location); asort($distances); print_r($distances); $closest = $location[key($distances)]; #print_r($closest); #echo "<br>Closest port from ARRAY is: " . $closest['features'][1]['properties']['Id'];
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 10
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 19
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 58
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 58
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 56
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 56
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 54
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 54
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 53
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 53
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 54
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 56
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 19
Branch analysis from position: 64
Branch analysis from position: 19
Branch analysis from position: 58
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 10
Branch analysis from position: 17
Branch analysis from position: 10
filename:       /in/JX0i5
function name:  (null)
number of ops:  79
compiled vars:  !0 = $json, !1 = $locations, !2 = $base_location, !3 = $distances, !4 = $c, !5 = $i, !6 = $z, !7 = $location, !8 = $key, !9 = $a, !10 = $b, !11 = $distance, !12 = $closest
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                       !0, '%0A%7B%0A%09%22type%22%3A+%22FeatureCollection%22%2C%0A%09%22features%22%3A+%5B%7B%0A%09%09%22type%22%3A+%22Feature%22%2C%0A%09%09%22geometry%22%3A+%7B%0A%09%09%09%22type%22%3A+%22Point%22%2C%0A%09%09%09%22coordinates%22%3A+%5B%0A%09%09%09%09-4.702540%2C%0A%09%09%09%0951.672791%0A%09%09%09%5D%0A%09%09%7D%2C%0A%09%09%22properties%22%3A+%7B%0A%09%09%09%22Id%22%3A+%221603%22%2C%0A%09%09%09%22Name%22%3A+%22TENBY%22%2C%0A%09%09%09%22Country%22%3A+%22Channel+Islands%22%2C%0A%09%09%09%22ContinuousHeightsAvailable%22%3A+true%0A%09%09%7D%0A%09%7D%2C+%7B%0A++++++%22type%22%3A+%22Feature%22%2C%0A++++++%22geometry%22%3A+%7B%0A++++++++%22type%22%3A+%22Point%22%2C%0A++++++++%22coordinates%22%3A+%5B%0A++++++++++-1.878530%2C%0A++++++++++50.721680%0A++++++++%5D%0A++++++%7D%2C%0A++++++%22properties%22%3A+%7B%0A++++++++%22Id%22%3A+%221603A%22%2C%0A++++++++%22Name%22%3A+%22Bournmouth%22%2C%0A++++++++%22Country%22%3A+%22Channel+Islands%22%2C%0A++++++++%22ContinuousHeightsAvailable%22%3A+true%0A++++++%7D%0A++++%7D%2C+%7B%0A++++++%22type%22%3A+%22Feature%22%2C%0A++++++%22geometry%22%3A+%7B%0A++++++++%22type%22%3A+%22Point%22%2C%0A++++++++%22coordinates%22%3A+%5B%0A++++++++++-2.35%2C%0A++++++++++49.433333%0A++++++++%5D%0A++++++%7D%2C%0A++++++%22properties%22%3A+%7B%0A++++++++%22Id%22%3A+%221603A%22%2C%0A++++++++%22Name%22%3A+%22Maseline+Pier%22%2C%0A++++++++%22Country%22%3A+%22Channel+Islands%22%2C%0A++++++++%22ContinuousHeightsAvailable%22%3A+true%0A++++++%7D%0A++++%7D%5D%0A%7D'
   53     1        INIT_FCALL                                                   'json_decode'
          2        SEND_VAR                                                     !0
          3        SEND_VAL                                                     <true>
          4        DO_ICALL                                             $14     
          5        ASSIGN                                                       !1, $14
   55     6        ASSIGN                                                       !2, <array>
   60     7        ASSIGN                                                       !3, <array>
   62     8        ASSIGN                                                       !4, 0
          9      > JMP                                                          ->13
   63    10    >   CONCAT                                               ~19     'I+%3D+', !4
         11        ECHO                                                         ~19
   62    12        PRE_INC                                                      !4
         13    >   FETCH_DIM_R                                          ~21     !1, 'features'
         14        COUNT                                                ~22     ~21
         15        IS_SMALLER                                                   !4, ~22
         16      > JMPNZ                                                        ~23, ->10
   67    17    >   ASSIGN                                                       !4, 0
         18      > JMP                                                          ->60
   68    19    > > FE_RESET_R                                           $25     !1, ->58
         20    > > FE_FETCH_R                                                   $25, !5, ->58
   69    21    > > FE_RESET_R                                           $26     !5, ->56
         22    > > FE_FETCH_R                                                   $26, !6, ->56
   70    23    >   FETCH_DIM_R                                          ~27     !6, 'geometry'
         24      > FE_RESET_R                                           $28     ~27, ->54
         25    > > FE_FETCH_R                                           ~29     $28, !7, ->54
         26    >   ASSIGN                                                       !8, ~29
   71    27        IS_EQUAL                                                     !8, 'coordinates'
         28      > JMPZ                                                         ~31, ->53
   73    29    >   FETCH_DIM_R                                          ~32     !2, 'lat'
         30        FETCH_DIM_R                                          ~33     !7, 1
         31        SUB                                                  ~34     ~32, ~33
         32        ASSIGN                                                       !9, ~34
   74    33        FETCH_DIM_R                                          ~36     !2, 'lng'
         34        FETCH_DIM_R                                          ~37     !7, 0
         35        SUB                                                  ~38     ~36, ~37
         36        ASSIGN                                                       !10, ~38
   75    37        INIT_FCALL                                                   'sqrt'
         38        POW                                                  ~40     !9, 2
         39        POW                                                  ~41     !10, 2
         40        ADD                                                  ~42     ~40, ~41
         41        SEND_VAL                                                     ~42
         42        DO_ICALL                                             $43     
         43        ASSIGN                                                       !11, $43
   76    44        ASSIGN_DIM                                                   !3, !4
         45        OP_DATA                                                      !11
   78    46        ROPE_INIT                                         5  ~47     'The+value+of+key+%27'
         47        ROPE_ADD                                          1  ~47     ~47, !8
         48        ROPE_ADD                                          2  ~47     ~47, '%27+is+%27'
         49        ROPE_ADD                                          3  ~47     ~47, !7
         50        ROPE_END                                          4  ~46     ~47, '%27'
         51        ECHO                                                         ~46
         52        ECHO                                                         '%0A'
   70    53    > > JMP                                                          ->25
         54    >   FE_FREE                                                      $28
   69    55      > JMP                                                          ->22
         56    >   FE_FREE                                                      $26
   68    57      > JMP                                                          ->20
         58    >   FE_FREE                                                      $25
   67    59        PRE_INC                                                      !4
         60    >   FETCH_DIM_R                                          ~51     !1, 'features'
         61        COUNT                                                ~52     ~51
         62        IS_SMALLER                                                   !4, ~52
         63      > JMPNZ                                                        ~53, ->19
   82    64    >   INIT_FCALL                                                   'print_r'
         65        SEND_VAR                                                     !2
         66        DO_ICALL                                                     
   84    67        INIT_FCALL                                                   'asort'
         68        SEND_REF                                                     !3
         69        DO_ICALL                                                     
   85    70        INIT_FCALL                                                   'print_r'
         71        SEND_VAR                                                     !3
         72        DO_ICALL                                                     
   86    73        INIT_FCALL                                                   'key'
         74        SEND_VAR                                                     !3
         75        DO_ICALL                                             $57     
         76        FETCH_DIM_R                                          ~58     !7, $57
         77        ASSIGN                                                       !12, ~58
   88    78      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
167.64 ms | 1639 KiB | 18 Q