1

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?

1
  • Can You Send Me Your Json .. Commented Jun 26, 2017 at 10:00

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Jaydeep Mor but this code didn't work. It's give this error "Warning: array_merge(): Argument #1 is not an array in /index.php on line 6" $decode_all = array_merge($decode1,$decode2);
@Johny your i think you check your $decode1 is Array or not.
0

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 :

enter image description here

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.