steven smith steven smith - 5 months ago 11
JSON Question

Why cant I use dots in arrays?

I wanted to try something out with an API from a company. I wanted to put their data into my database. But I've run into a problem. The API has one JSON object called "venue.country". But I can't use dots in my array for some reason?

Here is the code:

foreach ($data[1]['hits']['hits'] as $row) {
$statement->execute(array(
"name" => $row['fields']['name'][0],
"date" => $row['fields']['start'][0],
"lang_long" => "test",
It is this one! -> "country" => $row['fields']['venue.country'][0],
"genres" => $row['fields']['genres'][0],
"desc" => $row['fields']['description'][0],
"logo" => $row['fields']['logo'][0],
"header_pic" => $row['fields']['header'][0]
));
}


\everything else works in the PHP
And here is a piece of the JSON:

venue.country: [
"Nederland"
],


How can I get this to work? Can I use the "explode" method?

This is the full JSON:
https://hugo.events/genre/pop/next/200/120

Thanks.

EDIT:
Even with the answer @Prototype Chain gave it is giving me this error:
Notice: Undefined index: venue in /home/ubuntu/workspace/pop.php on line 18

Answer

The code works fine. After debugging I found that, there is no key/index venue.country for the array elements $data[1]['hits']['hits'][50] and $data[1]['hits']['hits'][51]. Since there is no index, its throwing notice.

You can add @before the variable to suppress the notice or check the condition if the index exixts.

"country" => @$row['fields']['venue.country'][0],

"country" => isset($row['fields']['venue.country']) ? $row['fields']['venue.country'][0] : '',

// run this code for testing.

error_reporting(E_ALL);
ini_set('display_errors', 1);
$jsondata = file_get_contents('https://hugo.events/genre/pop/next/200/100');
$data     = json_decode($jsondata, true);
foreach ($data[1]['hits']['hits'] as $row) {
   // echo $row['fields']['name'][0].'=>'.@$row['fields']['venue.country'][0].'<br/>';
   $array[]= array(
        "name" => $row['fields']['name'][0],
        "country" => isset($row['fields']['venue.country']) ? $row['fields']['venue.country'][0] : ''
        );
}
echo "<pre>";
print_r($array);