Récupérer une valeur dans un array qui est lui même dans un array
Répondre à la discussion
Affichage des résultats 1 à 3 sur 3

Récupérer une valeur dans un array qui est lui même dans un array



  1. #1
    invitea1400dd5

    Récupérer une valeur dans un array qui est lui même dans un array


    ------

    Bonsoir,

    J'ai un petit souci pour récupérer la valeur dans un array qui est lui même dans un autre array ... le tout en php.
    Mon but étant de récupérer le code postal de coordonnées Gps.

    Avec l'outil geocode de google map en bas de page voici ce que j'obtiens avec la fonction suivante :

    J'utilise une fonction qui me permet de récupérer tout ce code json
    Code:
    function getaddress($lat,$lng) {
    	$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
    	$json = file_get_contents($url);
    	$data=json_decode($json);
    	$status = $data->status; // Recupère si reponse existe via la fonction status de google
    	if($status=="OK") {
    		return $data->results[0]->address_components;
    	}
    	else
    		return false;
    }
    Je peux afficher mes valeurs de retour ici :
    Code:
    $lng= 5.1802;
    $lat= 43.59901;
    $adresse = getaddress($lat,$lng);
    print_r($adresse);
    Mais comment retenir que la valeur du code postal ? Et au passage dans une autre variable la valeur du pays ?

    Merci d'avance pour votre aide ...


    Pour ceux qui voudrait seulement retenir en valeur l'adresse complète voici la fonction :
    Code:
    function getaddress($lat,$lng) {
    	$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
    	$json = file_get_contents($url);
    	$data=json_decode($json);
    	$status = $data->status; // Recupère si reponse existe via la fonction status de google
    	if($status=="OK") {
    		return $data->results[1]->formatted_address;
    	}
    	else
    		return false;
    }

    Code:
    {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "Bois Vieux",
                   "short_name" : "Bois Vieux",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Saint-Pantaléon-les-Vignes",
                   "short_name" : "Saint-Pantaléon-les-Vignes",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Drôme",
                   "short_name" : "26",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "26770",
                   "short_name" : "26770",
                   "types" : [ "postal_code" ]
                }
             ],
             "formatted_address" : "Bois Vieux, 26770 Saint-Pantaléon-les-Vignes, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 44.4039069,
                      "lng" : 5.039973
                   },
                   "southwest" : {
                      "lat" : 44.40191799999999,
                      "lng" : 5.037302
                   }
                },
                "location" : {
                   "lat" : 44.4037279,
                   "lng" : 5.037637399999999
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 44.4042614302915,
                      "lng" : 5.039986480291502
                   },
                   "southwest" : {
                      "lat" : 44.4015634697085,
                      "lng" : 5.037288519708497
                   }
                }
             },
             "types" : [ "route" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "Saint-Pantaléon-les-Vignes",
                   "short_name" : "Saint-Pantaléon-les-Vignes",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Drôme",
                   "short_name" : "26",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Saint-Pantaléon-les-Vignes, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 44.40888899999999,
                      "lng" : 5.071002
                   },
                   "southwest" : {
                      "lat" : 44.3810789,
                      "lng" : 5.013318
                   }
                },
                "location" : {
                   "lat" : 44.3976309,
                   "lng" : 5.044534
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 44.40888899999999,
                      "lng" : 5.071002
                   },
                   "southwest" : {
                      "lat" : 44.3810789,
                      "lng" : 5.013318
                   }
                }
             },
             "types" : [ "locality", "political" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "26770",
                   "short_name" : "26770",
                   "types" : [ "postal_code" ]
                },
                {
                   "long_name" : "Taulignan",
                   "short_name" : "Taulignan",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Drôme",
                   "short_name" : "26",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "26770 Taulignan, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 44.513986,
                      "lng" : 5.1070835
                   },
                   "southwest" : {
                      "lat" : 44.3812057,
                      "lng" : 4.920115099999999
                   }
                },
                "location" : {
                   "lat" : 44.4599281,
                   "lng" : 4.997446099999999
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 44.513986,
                      "lng" : 5.1070835
                   },
                   "southwest" : {
                      "lat" : 44.3812057,
                      "lng" : 4.920115099999999
                   }
                }
             },
             "postcode_localities" : [
                "Aleyrac",
                "Le Pègue",
                "Montbrison-sur-Lez",
                "Roche-Saint-Secret-Béconne",
                "Rousset-les-Vignes",
                "Saint-Pantaléon-les-Vignes",
                "Salles-Sous-Bois",
                "Taulignan"
             ],
             "types" : [ "postal_code" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "Drôme",
                   "short_name" : "26",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Drôme, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 45.343976,
                      "lng" : 5.830446
                   },
                   "southwest" : {
                      "lat" : 44.115494,
                      "lng" : 4.6468618
                   }
                },
                "location" : {
                   "lat" : 44.73118960000001,
                   "lng" : 5.2266675
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 45.343976,
                      "lng" : 5.830446
                   },
                   "southwest" : {
                      "lat" : 44.115494,
                      "lng" : 4.6468618
                   }
                }
             },
             "types" : [ "administrative_area_level_2", "political" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Rhône-Alpes, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 46.519953,
                      "lng" : 7.1855661
                   },
                   "southwest" : {
                      "lat" : 44.115493,
                      "lng" : 3.6889129
                   }
                },
                "location" : {
                   "lat" : 45.1695797,
                   "lng" : 5.4502821
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 46.519953,
                      "lng" : 7.1855661
                   },
                   "southwest" : {
                      "lat" : 44.115493,
                      "lng" : 3.6889129
                   }
                }
             },
             "types" : [ "administrative_area_level_1", "political" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 51.089166,
                      "lng" : 9.560067799999999
                   },
                   "southwest" : {
                      "lat" : 41.3423276,
                      "lng" : -5.141227900000001
                   }
                },
                "location" : {
                   "lat" : 46.227638,
                   "lng" : 2.213749
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 51.089166,
                      "lng" : 8.233549099999999
                   },
                   "southwest" : {
                      "lat" : 42.333014,
                      "lng" : -4.795341899999999
                   }
                }
             },
             "types" : [ "country", "political" ]
          }
       ],
       "status" : "OK"
    }

    -----

  2. #2
    invited9b9018b

    Re : Récupérer une valeur dans un array qui est lui même dans un array

    Bonsoir,

    Un print_r(json_decode($json)) m'aiderait plus que le json lui même, je ne sais pas par coeur comment PHP réalise la transformation.
    Mais ce qui est clair c'est que "formatted_address" n'est pas la bonne variable.
    Il faudra boucler sur tous les éléments de "address_components"
    Le code postal est contenu dans l'élement de "address_components" qui possède le type "postal_code".
    (et le pays dans l'élément de type "country"....)
    A+

  3. #3
    invitea1400dd5

    Re : Récupérer une valeur dans un array qui est lui même dans un array

    Effectivement tu as raison ...
    Voici le array en question et merci de ton aide !
    Code:
    http://maps.googleapis.com/maps/api/geocode/json?latlng=45.09249,6.07286&sensor=false
    Code:
    function getaddress($lat,$lng) {
    	$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
    	$json = file_get_contents($url);
    	$data=json_decode($json);
    	$status = $data->status; // Recupère si reponse existe via la fonction status de google
    	if($status=="OK") {
    		//return $data->results[1]->formatted_address; // Si ne fonctionne pas le 2
    		return $data->results[0]->address_components;
    	}
    	else
    		return false;
    }
    Retour du print_r :
    Code:
    Array ( [0] => stdClass Object ( [long_name] => 12 [short_name] => 12 [types] => Array ( [0] => street_number ) ) [1] => stdClass Object ( [long_name] => Avenue du Rif Nel [short_name] => Avenue du Rif Nel [types] => Array ( [0] => route ) ) [2] => stdClass Object ( [long_name] => Huez [short_name] => Huez [types] => Array ( [0] => locality [1] => political ) ) [3] => stdClass Object ( [long_name] => Isère [short_name] => 38 [types] => Array ( [0] => administrative_area_level_2 [1] => political ) ) [4] => stdClass Object ( [long_name] => Rhône-Alpes [short_name] => RA [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [5] => stdClass Object ( [long_name] => France [short_name] => FR [types] => Array ( [0] => country [1] => political ) ) [6] => stdClass Object ( [long_name] => 38750 [short_name] => 38750 [types] => Array ( [0] => postal_code ) ) )
    Pour cet affichage :
    Code:
    {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "12",
                   "short_name" : "12",
                   "types" : [ "street_number" ]
                },
                {
                   "long_name" : "Avenue du Rif Nel",
                   "short_name" : "Avenue du Rif Nel",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Huez",
                   "short_name" : "Huez",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Isère",
                   "short_name" : "38",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "38750",
                   "short_name" : "38750",
                   "types" : [ "postal_code" ]
                }
             ],
             "formatted_address" : "12 Avenue du Rif Nel, 38750 Huez, France",
             "geometry" : {
                "location" : {
                   "lat" : 45.0934357,
                   "lng" : 6.071665899999999
                },
                "location_type" : "ROOFTOP",
                "viewport" : {
                   "northeast" : {
                      "lat" : 45.0947846802915,
                      "lng" : 6.073014880291502
                   },
                   "southwest" : {
                      "lat" : 45.0920867197085,
                      "lng" : 6.070316919708497
                   }
                }
             },
             "types" : [ "street_address" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "Jeux",
                   "short_name" : "Jeux",
                   "types" : [ "neighborhood", "political" ]
                },
                {
                   "long_name" : "Huez",
                   "short_name" : "Huez",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Isère",
                   "short_name" : "38",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "38750",
                   "short_name" : "38750",
                   "types" : [ "postal_code" ]
                }
             ],
             "formatted_address" : "Jeux, 38750 Huez, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 45.0932772,
                      "lng" : 6.075986599999999
                   },
                   "southwest" : {
                      "lat" : 45.0890355,
                      "lng" : 6.0667705
                   }
                },
                "location" : {
                   "lat" : 45.0907724,
                   "lng" : 6.071054699999999
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 45.0932772,
                      "lng" : 6.075986599999999
                   },
                   "southwest" : {
                      "lat" : 45.0890355,
                      "lng" : 6.0667705
                   }
                }
             },
             "types" : [ "neighborhood", "political" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "Huez",
                   "short_name" : "Huez",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Isère",
                   "short_name" : "38",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Huez, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 45.12394399999999,
                      "lng" : 6.120984
                   },
                   "southwest" : {
                      "lat" : 45.070562,
                      "lng" : 6.04158
                   }
                },
                "location" : {
                   "lat" : 45.081164,
                   "lng" : 6.057818999999999
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 45.12394399999999,
                      "lng" : 6.120984
                   },
                   "southwest" : {
                      "lat" : 45.070562,
                      "lng" : 6.04158
                   }
                }
             },
             "types" : [ "locality", "political" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "38750",
                   "short_name" : "38750",
                   "types" : [ "postal_code" ]
                },
                {
                   "long_name" : "Huez",
                   "short_name" : "Huez",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Isère",
                   "short_name" : "38",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "38750 Huez, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 45.1238922,
                      "lng" : 6.1208092
                   },
                   "southwest" : {
                      "lat" : 45.0706154,
                      "lng" : 6.041387299999999
                   }
                },
                "location" : {
                   "lat" : 45.0915292,
                   "lng" : 6.0678427
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 45.1238922,
                      "lng" : 6.1208092
                   },
                   "southwest" : {
                      "lat" : 45.0706154,
                      "lng" : 6.041387299999999
                   }
                }
             },
             "types" : [ "postal_code" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "Isère",
                   "short_name" : "38",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Isère, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 45.883397,
                      "lng" : 6.359309
                   },
                   "southwest" : {
                      "lat" : 44.695873,
                      "lng" : 4.7425939
                   }
                },
                "location" : {
                   "lat" : 44.9957745,
                   "lng" : 5.9293476
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 45.883397,
                      "lng" : 6.359309
                   },
                   "southwest" : {
                      "lat" : 44.695873,
                      "lng" : 4.7425939
                   }
                }
             },
             "types" : [ "administrative_area_level_2", "political" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "Rhône-Alpes",
                   "short_name" : "RA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Rhône-Alpes, France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 46.519953,
                      "lng" : 7.1855661
                   },
                   "southwest" : {
                      "lat" : 44.115493,
                      "lng" : 3.6889129
                   }
                },
                "location" : {
                   "lat" : 45.1695797,
                   "lng" : 5.4502821
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 46.519953,
                      "lng" : 7.1855661
                   },
                   "southwest" : {
                      "lat" : 44.115493,
                      "lng" : 3.6889129
                   }
                }
             },
             "types" : [ "administrative_area_level_1", "political" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "France",
                   "short_name" : "FR",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "France",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 51.089166,
                      "lng" : 9.560067799999999
                   },
                   "southwest" : {
                      "lat" : 41.3423276,
                      "lng" : -5.141227900000001
                   }
                },
                "location" : {
                   "lat" : 46.227638,
                   "lng" : 2.213749
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 51.089166,
                      "lng" : 8.233549099999999
                   },
                   "southwest" : {
                      "lat" : 42.333014,
                      "lng" : -4.795341899999999
                   }
                }
             },
             "types" : [ "country", "political" ]
          }
       ],
       "status" : "OK"
    }

Discussions similaires

  1. Recherche dans un Array en php
    Par invitea1400dd5 dans le forum Programmation et langages, Algorithmique
    Réponses: 2
    Dernier message: 20/10/2013, 19h38
  2. array c#
    Par invite754f3790 dans le forum Programmation et langages, Algorithmique
    Réponses: 3
    Dernier message: 31/10/2012, 20h02
  3. [Génétique] CGH array
    Par invitea75feaa5 dans le forum Biologie
    Réponses: 0
    Dernier message: 22/11/2009, 16h18
  4. [Génétique] CGH-array
    Par invite8b58690f dans le forum Biologie
    Réponses: 10
    Dernier message: 10/07/2008, 14h30
  5. [Biologie Cellulaire] cgh array
    Par invite54c908a4 dans le forum Biologie
    Réponses: 1
    Dernier message: 08/06/2008, 19h07