user12370483
user12370483

Reputation:

How to use 2 jsons file in 1 page

i'm trying to use 2 jsons to get different information.

One has the main information, and another json has the "audio" files.

My main json is something like this:

{  "featured": [
    {
      "id": "CID_475_Athena_Commando_M_Multibot",
      "name": "Líder da Equipe Mech",
      "image": "--IMAGE LINK--"
    },
    {
      "id": "Pickaxe_ID_236_Multibot1H",
      "name": "Cutelos Duplos",
      "image": "--IMAGE LINK--"
    },
]
}

And the other is

{  "sounds": [
    {
      "id": "CID_475_Athena_Commando_M_Multibot",
      "soundmp3": "--SOUND LINK--"
    },
    {
      "id": "Pickaxe_ID_236_Multibot1H",
      "soundmp3": "--SOUND LINK--"
    }
]
}

In my .php i'm using, but i can't manage to make it work.

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "---JSON URL---",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => false,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));

$response = curl_exec($curl);
$getinfo = json_decode($response, true);


$string = file_get_contents("sounds_id.json");
$json_sons = json_decode($string, true);

?>
<?php 
foreach ( $getinfo['featured'] as $jsonmain ) 
    foreach ( $json_sons['sons'] as $jsonsounds ) :  ?>

<?php
        if ($jsonmain['id'] == $jsonsons['id']) { ?>
            <?php echo $jsonmain['name']; ?> has the sound <a href="<?php echo $jsonsons['sounds']['soundmp3'] ?>">here.</a>

?>
<?php
        }
?>

<?php endforeach; ?>


The problem is that the main json it's from an external api, therefore i can't add the audio inside them. And the json changes all days. So in my "sound" json has always the items/sounds that i will be adding.

Thank you for your time.

Upvotes: 0

Views: 53

Answers (1)

mahmoud nassar
mahmoud nassar

Reputation: 11

Suggestion: you can reformat your sound JSON file for example :

{
 "CID_475_Athena_Commando_M_Multibot" : "Sound Link"
}

and after that you can load it in array

<?php
   $response = curl_exec($curl);
   $getinfo = json_decode($response, true);


   $string = file_get_contents("sounds_id.json");
   $json_sons = json_decode($string, true);

   foreach ( $getinfo['featured'] as $jsonmain ) {
      // HERE Your logic and 
      // you can get sound 
     $soundId = $jsonmain["id"];
     $soundUrl = isset($json_sons[$soundId])?$json_sons[$soundId] : null ; 
     // complete your logic  
    }

?>

I didn't test it but hopefully this idea help you

Upvotes: 1

Related Questions