-1

I have a json file that i need to parse to php and display only some values that i need

<?php
// Read JSON file
$readjson = file_get_contents('output.json') ;

//Decode JSON
$data = json_decode($readjson, true);

//Print data
echo "<br/><br/> Employee names are: <br/>";

//Parse the employee name
foreach ($data as $emp) {
  echo $emp['category.name']."<br/>";
}
?>

The output.json file contains some values ex:

    {
   "bizCode":10000,
   "message":"0#0",
   "data":[
      {
         "id":"sr:tournament:27070",
         "events":[
            {
               "homeTeamName":"Independiente Santa Fe",
               "awayTeamName":"America de Cali",
               "sport":{
                  "name":"Football",
                  "category":{
                     "name":"Colombia",
                     "tournament":{
                        "name":"Primera A, Apertura"
                     }
                  }
               },
            },
            {
               "homeTeamName":"AD Cali",
               "awayTeamName":"Millonarios FC",
               "sport":{
                  "name":"Football",
                  "category":{
                     "name":"Colombia",
                     "tournament":{
                        "name":"Primera A, Apertura"
                     }
                  }
               },
            }
         ],
         "categoryName":"Colombia",
      }
   ]
}

I have test it but it's not showing any data and i can't find the issue. Maybe the problem is that i cant locate the exact path of the data?

  • That's exactly your problem. The data you want is under `'events'`, not `'data'`, so you have to go a level deeper. Also, this is not valid PHP syntax: `$emp['category.name']` - you seem to be mixing JS into this. The proper PHP array access on multiple indices is placing those indices one after another: `$emp['category']['name']`. – El_Vanja Dec 24 '20 at 10:27
  • It might also be worth mentioning that you say "employee names" in your question, but your JSON seems to be holding football match data. Maybe you've shown the wrong JSON example? Or your code is not pulling the correct file? – El_Vanja Dec 24 '20 at 10:29
  • @El_Vanja the json and code is correct i just put some text there from my other project that worked before. I have changed the code to here `// Read JSON file $readjson = file_get_contents('output.json') ; //Decode JSON $events = json_decode($readjson, true); //Print data echo "

    Print:
    "; //Parse the employee name foreach ($events as $emp) { echo $emp['category']['name']."
    "; }`
    – user2178344 Dec 24 '20 at 10:36
  • Please add any changes to the question by editing it (the `edit` link can be found at the bottom of your post), rather than in the comments. – El_Vanja Dec 24 '20 at 10:40
  • Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – El_Vanja Dec 24 '20 at 10:40
  • @El_Vanja Still not working no data is displayed – user2178344 Dec 24 '20 at 10:40
  • 1
    That's because you ignored the first half of my comment. You're not iterating at the right level. Read the linked question, it has detailed instructions. – El_Vanja Dec 24 '20 at 10:41
  • @El_Vanja i already mention that i know i'm not pointing at the right level, thats why i asked maybe you can help – user2178344 Dec 24 '20 at 11:24
  • It's not that difficult to find examples of [iterating a multidimensional array](https://stackoverflow.com/questions/1551822/looping-a-multidimensional-array-in-php). – El_Vanja Dec 24 '20 at 11:32

1 Answers1

0

Your json format is wrong,wrong format, you can validate using any tool like this check here.also i formatted for you{"bizCode":10000,"message":"0#0","data":[{"id":"sr:tournament:27070","events":[{"homeTeamName":"Independiente Santa Fe","awayTeamName":"America de Cali","sport":{"name":"Football","category":{"name":"Colombia","tournament":{"name":"Primera A, Apertura"}}}},{"homeTeamName":"AD Cali","awayTeamName":"Millonarios FC","sport":{"name":"Football","category":{"name":"Colombia","tournament":{"name":"Primera A, Apertura"}}}}]}]}

you can replace this as your json file and print_r($data)

Nathan
  • 1
  • 2