Reputation: 45
I want to parse json from multiple url. I can parse an url with this code:
<?php
$url1 = file_get_contents("http://www.url1.com");
$url2 = file_get_contents("http://www.url2.com");
$decode = json_decode($url1);
foreach( $decode as $obj1 ) {
foreach( $obj1 as $obj2 ) {
foreach( $obj2 as $obj3 ) {
foreach( $obj3 as $obj4 ) {
echo $obj4->name . '<br />';
}
}
}
}
?>
I want to parse url1 and url2. How can I do that?
Upvotes: 1
Views: 750
Reputation: 5092
Myjson1.json File
{
"name": "xyz",
"city": "bla bla",
"state": "bla bla bla"
}
Myjson2.json File
{
"country": "bla bla",
"postal": "000000"
}
MyPhp.php File
<?php
$url1 = "..\htdocs\myjson1.json";
$json1 = file_get_contents($url1);
$decode1 = json_decode($json1, TRUE);
$url2 = "..\htdocs\myjson2.json";
$json2 = file_get_contents($url2);
$decode2 = json_decode($json2, TRUE);
print '<pre>';
print_r($decode1);
print_r($decode2);
print '<pre> <hr>';
$merge_array = array_merge($decode1,$decode2);
print_r($merge_array);
//http://php.net/manual/en/function.extract.php
extract($merge_array, EXTR_PREFIX_SAME, "wddx");
echo "<h1> $name </h1>";
echo "<h1> $city <h1>";
echo "<h1> $state <h1>";
echo "<h1> $country <h1>";
echo "<h1> $postal <h1>";
?>
OUTPUT :
NOTE: wddx_deserialize
Reference : http://php.net/manual/en/function.extract.php
OR ( 2nd Way)
<?php
$decode1 = array("name"=>"xyz","city"=>"bla bla");
$decode2 = array("state"=>"xyz","country"=>"India");
$merge_array = array_merge($decode1,$decode2);
foreach($merge_array as $mydata) {
echo '<h2>'. $mydata .'</h2>';
}
?>
Demo: https://eval.in/822392
Upvotes: 0
Reputation: 1703
I think you can use array_merge()
after decode json
.
<?php
$url1 = file_get_contents("json1.json");
$url2 = file_get_contents("json2.json");
$decode1 = json_decode($url1);
$decode2 = json_decode($url2);
$decode_all = array_merge($decode1,$decode2);
foreach( $decode_all as $obj1 ) {
echo $obj1->name . '<br />';
}
?>
Output
Jaydeep
Manish
Upvotes: 1