IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Récupération de données d'une chaine


Sujet :

Langage PHP

  1. #1
    Membre à l'essai
    Homme Profil pro
    Alternance
    Inscrit en
    Juin 2013
    Messages
    21
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Alternance
    Secteur : Transports

    Informations forums :
    Inscription : Juin 2013
    Messages : 21
    Points : 22
    Points
    22
    Par défaut Récupération de données d'une chaine
    Bonjour,

    Via un cURL je récupère une string sous ce format:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    {"request":{"carrier":{"requestedCode":"AF","fsCode":"AF"},"flightNumber":{"requested":"6152","interpreted":"6152"},"airportOrMetro":{"airport":{},"metro":{}},"date":{"year":"2013","month":"6","day":"20","interpreted":"2013-06-20"},"days":{"interpreted":1},"codeshareType":{},"serviceType":{},"flightType":{"interpreted":"DIRECT"},"codeType":{},"extendedOptions":{},"url":"https://api.flightstats.com/flex/connections/rest/v1/json/direct/flight/AF/6152/departing/2013/06/20"},"appendix":{"airlines":[{"fs":"AF","iata":"AF","icao":"AFR","name":"Air France","phoneNumber":"1-800-237-2747","active":true}],"airports":[{"fs":"TLS","iata":"TLS","icao":"LFBO","name":"Blagnac Airport","city":"Toulouse","cityCode":"TLS","countryCode":"FR","countryName":"France","regionName":"Europe","timeZoneRegionName":"Europe/Paris","localTime":"2013-06-20T13:34:31.207","utcOffsetHours":2.0,"latitude":43.630071,"longitude":1.374321,"elevationFeet":499,"classification":2,"active":true},{"fs":"ORY","iata":"ORY","icao":"LFPO","name":"Paris Orly Airport","street1":"94396 Orly Aérogare","city":"Paris","cityCode":"PAR","countryCode":"FR","countryName":"France","regionName":"Europe","timeZoneRegionName":"Europe/Paris","localTime":"2013-06-20T13:34:31.207","utcOffsetHours":2.0,"latitude":48.728283,"longitude":2.3597,"elevationFeet":292,"classification":1,"active":true}],"equipments":[{"iata":"320","name":"Airbus Industrie A320","turboProp":false,"jet":true,"widebody":false,"regional":false}]},"flights":[{"departureAirportFsCode":"ORY","arrivalAirportFsCode":"TLS","departureDateFrom":"2013-06-03","departureDateTo":"2013-07-12","departureDaysOfWeek":[1,2,3,4,5],"arrivalDateAdjustment":0,"departureTime":"06:15:00.000","arrivalTime":"07:25:00.000","distanceMiles":356,"flightDurationMinutes":70,"layoverDurationMinutes":0,"flightType":"NON_STOP","serviceType":"PASSENGER_ONLY","online":true,"flightLegs":[{"departureAirportFsCode":"ORY","arrivalAirportFsCode":"TLS","departureTime":"06:15:00.000","arrivalTime":"07:25:00.000","departureDateAdjustment":0,"arrivalDateAdjustment":0,"departureTerminal":"W","carrierFsCode":"AF","flightNumber":"6152","codeshare":false,"equipmentCodes":["320"],"distanceMiles":356,"flightDurationMinutes":70,"layoverDurationMinutes":0}]}]}
    Voici une version arrangé avec indentation:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    {
     "request": {
      "carrier": {
       "requestedCode": "AF",
       "fsCode": "AF"
      },
      "flightNumber": {
       "requested": "6152",
       "interpreted": "6152"
      },
      "airportOrMetro": {
       "airport": {},
       "metro": {}
      },
      "date": {
       "year": "2013",
       "month": "6",
       "day": "20",
       "interpreted": "2013-06-20"
      },
      "days": {
       "interpreted": 1
      },
      "codeshareType": {},
      "serviceType": {},
      "flightType": {
       "interpreted": "DIRECT"
      },
      "codeType": {},
      "extendedOptions": {},
      "url": "https://api.flightstats.com/flex/connections/rest/v1/json/direct/flight/AF/6152/departing/2013/06/20"
     },
     "appendix": {
      "airlines": [
       {
        "fs": "AF",
        "iata": "AF",
        "icao": "AFR",
        "name": "Air France",
        "phoneNumber": "1-800-237-2747",
        "active": true
       }
      ],
      "airports": [
       {
        "fs": "TLS",
        "iata": "TLS",
        "icao": "LFBO",
        "name": "Blagnac Airport",
        "city": "Toulouse",
        "cityCode": "TLS",
        "countryCode": "FR",
        "countryName": "France",
        "regionName": "Europe",
        "timeZoneRegionName": "Europe/Paris",
        "localTime": "2013-06-20T09:40:21.547",
        "utcOffsetHours": 2,
        "latitude": 43.630071,
        "longitude": 1.374321,
        "elevationFeet": 499,
        "classification": 2,
        "active": true
       },
       {
        "fs": "ORY",
        "iata": "ORY",
        "icao": "LFPO",
        "name": "Paris Orly Airport",
        "street1": "94396 Orly Aérogare",
        "city": "Paris",
        "cityCode": "PAR",
        "countryCode": "FR",
        "countryName": "France",
        "regionName": "Europe",
        "timeZoneRegionName": "Europe/Paris",
        "localTime": "2013-06-20T09:40:21.547",
        "utcOffsetHours": 2,
        "latitude": 48.728283,
        "longitude": 2.3597,
        "elevationFeet": 292,
        "classification": 1,
        "active": true
       }
      ],
      "equipments": [
       {
        "iata": "320",
        "name": "Airbus Industrie A320",
        "turboProp": false,
        "jet": true,
        "widebody": false,
        "regional": false
       }
      ]
     },
     "flights": [
      {
       "departureAirportFsCode": "ORY",
       "arrivalAirportFsCode": "TLS",
       "departureDateFrom": "2013-06-03",
       "departureDateTo": "2013-07-12",
       "departureDaysOfWeek": [
        1,
        2,
        3,
        4,
        5
       ],
       "arrivalDateAdjustment": 0,
       "departureTime": "06:15:00.000",
       "arrivalTime": "07:25:00.000",
       "distanceMiles": 356,
       "flightDurationMinutes": 70,
       "layoverDurationMinutes": 0,
       "flightType": "NON_STOP",
       "serviceType": "PASSENGER_ONLY",
       "online": true,
       "flightLegs": [
        {
         "departureAirportFsCode": "ORY",
         "arrivalAirportFsCode": "TLS",
         "departureTime": "06:15:00.000",
         "arrivalTime": "07:25:00.000",
         "departureDateAdjustment": 0,
         "arrivalDateAdjustment": 0,
         "departureTerminal": "W",
         "carrierFsCode": "AF",
         "flightNumber": "6152",
         "codeshare": false,
         "equipmentCodes": [
          "320"
         ],
         "distanceMiles": 356,
         "flightDurationMinutes": 70,
         "layoverDurationMinutes": 0
        }
       ]
      }
     ]
    }
    Mon problème ? Je n'arrive pas à composé un algorithme pour récupérer les éléments distinctement. Exemple:
    J'ai une fonction avec laquelle j'envoie "departureTerminal" en argument, et dans mon cas je voudrais que ce me retourne "W".

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    function maFonction($objetVoulu){
    //instructions et traitements
    return $resultat;
    }
    Auriez-vous quelque propositions d'algorithme avec les outils de PHP qui vont avec? La structure des page reste la même, mais les données changent.

    En vous remerciant infiniment d'avance.

    Cordialement

    Brian

  2. #2
    Membre à l'essai
    Homme Profil pro
    Alternance
    Inscrit en
    Juin 2013
    Messages
    21
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Alternance
    Secteur : Transports

    Informations forums :
    Inscription : Juin 2013
    Messages : 21
    Points : 22
    Points
    22
    Par défaut
    Je viens de comprendre que c'est du format JSON ...

    Je mettrai la réponse une fois terminé.

  3. #3
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Points : 91 220
    Points
    91 220
    Billets dans le blog
    20

  4. #4
    Membre à l'essai
    Homme Profil pro
    Alternance
    Inscrit en
    Juin 2013
    Messages
    21
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Alternance
    Secteur : Transports

    Informations forums :
    Inscription : Juin 2013
    Messages : 21
    Points : 22
    Points
    22
    Par défaut
    Voici le code final pour ceux qui passeraient par là:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    /***** Traitement des données *****/
    	$resultat = json_decode($file_contents, true);
    	$typeDeVol = $resultat['request']['flightType']['interpreted'];
     
    	$ensembleCompany = $resultat['appendix']['airlines'];
     
    	$nomsCompany = array(); // Array Contenant l'ensemble des noms des compagnies aerienne
    	foreach ($ensembleCompany as $value){
    		array_push($nomsCompany,$value['name']);
    	}
     
    	$ensembleDesAeroports = $resultat['appendix']['airports'];
     
    	$arrayAeroports = array();
    	foreach ($ensembleDesAeroports as $value){
     
    		$aeroports = array();
    		if(isset( $value['iata'])){
    			$aeroports['code'] =  $value['iata'];
    		}
    		if(isset( $value['name'])){
    			$aeroports['nom'] = $value['name'];
    		}
    		if(isset( $value['street1'])){
    			$aeroports['adresse'] = $value['street1'];
    		}
    		if(isset( $value['city'])){
    			$aeroports['ville'] = $value['city'];
    		}
    		if(isset( $value['countryName'])){
    			$aeroports['pays'] = $value['countryName'];
    		}
     
    		$arrayAeroports[$value['iata']] = $aeroports;
     
     
    	}
     
    	$ensembleDesVols = $resultat['flights'];
     
    	$arrayOfFlights = array();
    	foreach ($ensembleDesVols as $value){	
    		$vol = array();
     
    		$aeroportDepart =  $arrayAeroports[$value['departureAirportFsCode']];
    		$aeroportArrive =  $arrayAeroports[$value['arrivalAirportFsCode']];
     
    		$flightLegs= $value['flightLegs'];
    		foreach ($flightLegs as $value2){
    			if($value2['departureAirportFsCode'] == $value['departureAirportFsCode'] && isset($value2['departureTerminal'])){
    				$terminalDepart = $value2['departureTerminal'];
    				$aeroportDepart['terminal'] = $terminalDepart;
    			} 			
    			if($value2['arrivalAirportFsCode'] == $value['arrivalAirportFsCode'] && isset($value2['arrivalTerminal'])){
    				$terminalArrive = $value2['arrivalTerminal'];
    				$aeroportArrive['terminal'] = $terminalArrive;
    			} 
    		}
     
    		$heureDepart = $value['departureTime'];
    		$heureArrive = $value['arrivalTime'];
    		$distance = 1.6*$value['distanceMiles']." km";
    		$temps = $value['flightDurationMinutes'];
     
    		$vol['aeroportDepart'] = $aeroportDepart;
    		$vol['aeroportArrive'] =$aeroportArrive;
    		$vol['heureDepart'] =substr($heureDepart, 0,8);
    		$vol['heureArrive'] =substr($heureArrive, 0,8);
    		$vol['distance'] =$distance;
    		$vol['temps'] =$temps;
    		array_push($arrayOfFlights,$vol);
    	}
     
    	$typeAvion = $resultat['appendix']['equipments'][0]['name'];
    	/** Création de la struture du tableau **/
    	$html = "";
    	$html = $html.$nomsCompany[0]."<br>";
    	$html = $html.$_POST['numFlight']."<br>";
    	$html = $html."<table>";
    		$html = $html."<tr>";
    			$html = $html."<th>D&eacute;part</th>";
    			$html = $html."<th>Arriv&eacute;</th>";
     
    		$html = $html."</tr>";
    		$html = $html."<tr>";
    		/*** Cr&eacute;ation de la partie d&eacute;part ***/
    			$html = $html."<td>";
    				$html = $html."<table>";
    					$html = $html."<tr>";
    						$html = $html."<td>";
    							$html = $html."A&eacute;roport de d&eacute;part:";
    						$html = $html."</td>";	
    						$html = $html."<td>";
    							$html = $html.$vol['aeroportDepart']['nom'];
    						$html = $html."</td>";
    					$html = $html."</tr>";	
     
    					$html = $html."<tr>";
    						$html = $html."<td>";
    							$html = $html."Adresse de d&eacute;part:";
    						$html = $html."</td>";	
    						$html = $html."<td>";
    							if(isset($vol['aeroportDepart']['adresse'])){
    								$html = $html.$vol['aeroportDepart']['adresse']." , ";
    							}
     
    							if(isset($vol['aeroportDepart']['ville'])){
    								$html = $html.$vol['aeroportDepart']['ville']." , ";
    							}
     
    							if(isset($vol['aeroportDepart']['ville'])){
    								$html = $html.$vol['aeroportDepart']['pays'];
    							}
    						$html = $html."</td>";
    					$html = $html."</tr>";
     
    					$html = $html."<tr>";
    						$html = $html."<td>";
    							$html = $html."Heure de d&eacute;part:";
    						$html = $html."</td>";	
    						$html = $html."<td>";
    							$html = $html.$vol['heureDepart'];
    						$html = $html."</td>";
    					$html = $html."</tr>";		
     
    					$html = $html."<tr>";
    						$html = $html."<td>";
    							$html = $html."Terminal de d&eacute;part:";
    						$html = $html."</td>";	
    						$html = $html."<td>";
    							if(isset($vol['aeroportDepart']['terminal'])){
    								$html = $html.$vol['aeroportDepart']['terminal'];
    							}else{
    								$html = $html."Non renseign&eacute;";
    							}
    						$html = $html."</td>";
    					$html = $html."</tr>";
    				$html = $html."</table>";
    			$html = $html."</td>";	
     
    	/*** Cr&eacute;ation de la partie arriv&eacute; ***/
    			$html = $html."<td>";
    				$html = $html."<table>";
    					$html = $html."<tr>";
    						$html = $html."<td>";
    							$html = $html."A&eacute;roport de d'arriv&eacute;:";
    						$html = $html."</td>";	
    						$html = $html."<td>";
    							$html = $html.$vol['aeroportArrive']['nom'];
    						$html = $html."</td>";
    					$html = $html."</tr>";	
     
    					$html = $html."<tr>";
    						$html = $html."<td>";
    							$html = $html."Adresse de d'arriv&eacute;:";
    						$html = $html."</td>";	
    						$html = $html."<td>";
    							if(isset($vol['aeroportArrive']['adresse'])){
    								$html = $html.$vol['aeroportArrive']['adresse']." , ";
    							}
     
    							if(isset($vol['aeroportArrive']['ville'])){
    								$html = $html.$vol['aeroportArrive']['ville']." , ";
    							}
     
    							if(isset($vol['aeroportArrive']['ville'])){
    								$html = $html.$vol['aeroportArrive']['pays'];
    							}
    						$html = $html."</td>";
    					$html = $html."</tr>";
     
    					$html = $html."<tr>";
    						$html = $html."<td>";
    							$html = $html."Heure de d'arriv&eacute;:";
    						$html = $html."</td>";	
    						$html = $html."<td>";
    							$html = $html.$vol['heureArrive'];
    						$html = $html."</td>";
    					$html = $html."</tr>";		
     
    					$html = $html."<tr>";
    						$html = $html."<td>";
    							$html = $html."Terminal de d'arriv&eacute;:";
    						$html = $html."</td>";	
    						$html = $html."<td>";
    							if(isset($vol['aeroportArrive']['terminal'])){
    								$html = $html.$vol['aeroportArrive']['terminal'];
    							}else{
    								$html = $html."Non renseign&eacute;";
    							}
    						$html = $html."</td>";
    					$html = $html."</tr>";
    				$html = $html."</table>";
    			$html = $html."</td>";
    		$html = $html."</tr>";
    	$html = $html."</table>";
     
    	$html = $html.'Distance parcourue: '.$vol['distance'].'<br>';
    	$html = $html.'Temps de vol: '.$vol['temps'].'mn<br>';
    	return $html;
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. récupération de données sur une disquette
    Par olilacta dans le forum Ordinateurs
    Réponses: 3
    Dernier message: 21/04/2006, 13h41
  2. extraction d'une donnée d'une chaine de caractères.
    Par LESOLEIL dans le forum Langage
    Réponses: 10
    Dernier message: 20/04/2006, 12h00
  3. Problème de récupérations de données dans une table mysql
    Par Helpine dans le forum SQL Procédural
    Réponses: 3
    Dernier message: 09/03/2006, 19h07
  4. récupération des données via une liste déroulante
    Par rahan_dave dans le forum Access
    Réponses: 1
    Dernier message: 13/10/2005, 12h27
  5. Réponses: 4
    Dernier message: 05/04/2004, 10h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo