Ответы пользователя по тегу JSON
  • Как извлечь нужную строку json в php?

    mzcoding
    @mzcoding
    Web-Разработка
    <?php
    
    
     $json = '{
      "response":
      {
        "GeoObjectCollection":
        {
          "metaDataProperty":
          {
            "GeocoderResponseMetaData":
            {
              "Point":
              {
                "pos": "30.245018 59.844872"
              },
              "request": "30.245018 59.844872",
              "results": "1",
              "found": "1"
            }
          },
          "featureMember": [
          {
            "GeoObject":
            {
              "metaDataProperty":
              {
                "GeocoderMetaData":
                {
                  "precision": "other",
                  "text": "Россия, Санкт-Петербург, Кировский район, муниципальный округ Дачное",
                  "kind": "district",
                  "Address":
                  {
                    "country_code": "RU",
                    "formatted": "Россия, Санкт-Петербург, Кировский район, муниципальный округ Дачное",
                    "Components": [
                    {
                      "kind": "country",
                      "name": "Россия"
                    },
                    {
                      "kind": "province",
                      "name": "Северо-Западный федеральный округ"
                    },
                    {
                      "kind": "province",
                      "name": "Санкт-Петербург"
                    },
                    {
                      "kind": "locality",
                      "name": "Санкт-Петербург"
                    },
                    {
                      "kind": "district",
                      "name": "Кировский район"
                    },
                    {
                      "kind": "district",
                      "name": "муниципальный округ Дачное"
                    }]
                  },
                  "AddressDetails":
                  {
                    "Country":
                    {
                      "AddressLine": "Россия, Санкт-Петербург, Кировский район, муниципальный округ Дачное",
                      "CountryNameCode": "RU",
                      "CountryName": "Россия",
                      "AdministrativeArea":
                      {
                        "AdministrativeAreaName": "Санкт-Петербург",
                        "Locality":
                        {
                          "LocalityName": "Санкт-Петербург",
                          "DependentLocality":
                          {
                            "DependentLocalityName": "Кировский район",
                            "DependentLocality":
                            {
                              "DependentLocalityName": "муниципальный округ Дачное"
                            }
                          }
                        }
                      }
                    }
                  }
                }
              },
              "name": "муниципальный округ Дачное",
              "description": "Кировский район, Санкт-Петербург, Россия",
              "boundedBy":
              {
                "Envelope":
                {
                  "lowerCorner": "30.208061 59.831309",
                  "upperCorner": "30.269183 59.858702"
                }
              },
              "Point":
              {
                "pos": "30.240194 59.845749"
              }
            }
          }]
        }
      }
    }';
    
    $result = json_decode($json, true);
    if (isset($result['response']['GeoObjectCollection']['featureMember'])) {
        foreach($result['response']['GeoObjectCollection']['featureMember'] as $featureMember) {
            if(isset($featureMember['GeoObject']['metaDataProperty']['GeocoderMetaData']['Address'])) {
                $address = $featureMember['GeoObject']['metaDataProperty']['GeocoderMetaData']['Address'];
                if(isset($address['Components']) && is_array($address['Components'])) {
                    foreach ($address['Components'] as  $component) {
                        echo ($component['kind'] ."-". $component['name']) . "\n\r";
                    }
                }
            }
        }
    }
    Ответ написан
    Комментировать