S
S
sochi-russia2015-01-02 16:17:49
PHP
sochi-russia, 2015-01-02 16:17:49

How to extract a specific value from Json?

Hello everyone, I'm back again, Happy New Year everyone, please tell me. There is a code

<?php 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.travelpayouts.com/v1/city-directions?origin=MOW&token=xxxxxxx&limit=10");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Access-Token: xxxxxx"));
$response = curl_exec($ch);
curl_close($ch);
 
$json=json_decode($response,true);
?>
<?php 
$products=json_decode($response,true);
 
foreach ($products as $key => $value) { 
    echo "<table class='mytable'><hr>";
     foreach ($value as $k=> $v) {
           echo "<tr><td><br>".$k."</td><td></td></tr>";
           foreach ($v as $g=> $l) { 
                $g=str_replace ('price','цена',$g); 
                     $g=str_replace ('airline','авиакомпания',$g);
                     $g=str_replace ('origin','город вылета',$g);
                     $g=str_replace ('destination','город прилета',$g);
                     $g=str_replace ('transfers','кол-во пересадок',$g);
                     $g=str_replace ('flight_number','номер рейса',$g);
                     $g=str_replace ('departure_at','дата вылета',$g);
                     $g=str_replace ('return_at','дата возвращения',$g);
                     $g=str_replace ('expires_at','Срок истечения актуальности цены',$g);
            
                     echo "<tr><td>".$g."</td><td>".$l."</td></tr>";
            } 
     }
     echo "</table>";  
}

?>

There are Json files, here they are : Country data
in json : api.travelpayouts.com/data/countries.json Cities
data in json format: api.travelpayouts.com/data/cities.json Airport
data in json format: api .travelpayouts.com/data/airports.json
Data about airlines in json format: api.travelpayouts.com/data/airlines.json
I need values ​​to be replaced from Json:
'airline', 'origin', 'destination',
here what the table looks like now:
a2789cc8382f416480fb65815840f2b1.jpg
Please show if possible using my code as an example.
Thank you in advance and have a good holiday mood, regards.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2015-01-03
@R0dger

Well, maybe I misunderstood .. but what stops you json_decode -> array['`you_key'] = 'You Value' ->json_encode

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question