1

I'm trying to extract data from the rome2rio (API http://www.rome2rio.com/documentation/search). They give me the possibility to get the details from a JSON or XML file on their server. The JSON output looks like this:

{
  "agencies":
    [{
    "code":       "SWISSRAILWAYS",
    "name":       "Swiss Railways (SBB/CFF/FFS)",
    "url":        "http://www.sbb.ch"
    "iconPath":   "/logos/trains/ch.png",
    "iconSize":   "27,23",
    "iconOffset": "0,0"
    ]},
  "routes":
    [{
    "name":     "Train",
    "distance": 95.92,
    "duration": 56,
    "stops":
      [{
      "name": "Bern",
      "pos":  "46.94926,7.43883",
      "kind": "station"
      },{
      "name": "Zürich HB",
      "pos":  "47.37819,8.54019",
      "kind": "station"
      }],
    "segments":
      [{
      "kind":     "train",
      "subkind":     "train",
      "isMajor":  1,
      "distance": 95.92,
      "duration": 56,
      "sName":    "Bern",
      "sPos":     "46.94938,7.43927",
      "tName":    "Zürich HB",
      "tPos":     "47.37819,8.54019",
      "path":     "{wp}Gu{kl@wb@uVo|AqiDyoBhUibDeiDc`AsmDaxBqk@wwA...",
      "indicativePrice":{
        "price":45,
        "currency":"USD",
        "isFreeTransfer":0,
        "nativePrice":40,
        "nativeCurrency":"CHF"
      },
      "itineraries":
        [{
        "legs":
          [{
          "url": "http://fahrplan.sbb.ch/bin/query.exe/en...",
          "hops":
            [{
            "distance":  95.92,
            "duration":  56,
            "sName":     "Bern",
            "sPos":      "46.94938,7.43927",
            "tName":     "Zürich HB",
            "tPos":      "47.37819,8.54019",
            "frequency": 400,
            "indicativePrice":{
              "price":45,
              "currency":"USD",
              "isFreeTransfer":0,
              "nativePrice":40,
              "nativeCurrency":"CHF"
            },
            "lines":
              [{
              "name":      "",
              "vehicle":   "train",
              "agency":    "SWISSRAILWAYS",
              "frequency": 400,
              "duration":  57,
              }]
            }]
          }]
        }]
      }]
    }]
  }]
}

I want to extract the 'segments' data via PHP and use this code:

<?php 
$url = 'http://free.rome2rio.com/api/1.2/json/Search?key=vwiC3pvW&oName=Germany&dName=Yemen';
$content = file_get_contents($url);
$json = json_decode($content, true);
?>

<html>
<head>
<style>
section {height:500px; width:1000px; color:red;}
</style>


</head>
<body> 
<section>

<?php

	foreach($json as $i){ 
			echo "$i[segments]</br>"; 
			}
 ?>
 
</section>
</body>
<

When I execute the code I get this result:

Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21


Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21


Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21


Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21


Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21


Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21

Thanks in advance for any kind of help!

1 Answer 1

1

I dont know from where is the "segment"index.. But you can try this:

<?php foreach($json as $segment => $i):?>
    <h3><?php echo $segment ?></h3>
    <?php foreach($i as $index => $value): ?>
    <p><?php echo $index.": ".$value ?></p>
    <?php endforeach; ?>
<?php endforeach; ?>

OR you can use Itinaretor... I think it's a better solucion..

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json));
foreach($iterator as $key => $value) {
    echo "<p>$key => $value</p>";
}

Reference: PHP foreach() with arrays within arrays?

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a possibility to do s.th. like this? 'code' $iterator = new RecursiveIteratorIterator(new 'code' RecursiveArrayIterator($json)); foreach($iterator as $key => $value) { echo array_search("price",$json,true); } 'code'
if you want get the price of something use this: if($key == 'price') echo "Price: ".$value;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.