Php 为什么我不能在数组中使用点?

Php 为什么我不能在数组中使用点?,php,json,Php,Json,我想用一家公司的API尝试一下。我想把他们的数据输入我的数据库。但我遇到了一个问题。API有一个JSON对象,名为“venue.country”。但是由于某种原因我不能在数组中使用点 代码如下: foreach ($data[1]['hits']['hits'] as $row) { $statement->execute(array( "name" => $row['fields']['name'][0], "

我想用一家公司的API尝试一下。我想把他们的数据输入我的数据库。但我遇到了一个问题。API有一个JSON对象,名为“venue.country”。但是由于某种原因我不能在数组中使用点

代码如下:

    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]
        ));
    }
\其他一切都在PHP中工作 下面是JSON的一部分:

venue.country: [
"Nederland"
],
我怎样才能让它工作?我可以使用“爆炸”方法吗

这是完整的JSON:

谢谢

编辑:
即使是@Prototype Chain给出的答案,它也给了我这个错误:
注意:未定义的索引:第18行的/home/ubuntu/workspace/pop.php中的vention

$row['fields']['venue']['country'][0]

在没有看到JSON数据的全部内容的情况下进行调试有点困难,然而,在过去,当我在解析JSON或XML数据时遇到困难,而这些数据的源格式很奇怪,我发现在解析数据之前对数据执行预处理str_replace()非常有用

所以类似这样的东西可能适合你:

$input = some_function_to_get_string_data_from_api();
$input = str_replace( "venue.country", "venuecountry", $input );
$json  = json_encode( $input );

这不是一个优雅的解决方案,如果您处理大量需要修改的字段,它会很快崩溃,但是如果您只处理一两个给您带来麻烦的字段,它可能是一个快速而肮脏的补丁。

鉴于您的示例JSON数据,此代码运行良好:

$string = <<<EOT
  {INSERT YOUR JSON STRING HERE}
EOT;

$json = json_decode( $string, true );
echo $json[1]['hits']['hits'][0]['fields']['venue.location'][0] . "\n";
echo $json[1]['hits']['hits'][0]['fields']['venue.location'][1] . "\n";

$string=代码运行良好。调试后,我发现数组元素$data[1]['hits']['hits'][50]和$data[1]['hits']['hits'][51]没有键/索引vention.country。由于没有索引,它会抛出通知

您可以在变量之前添加@以抑制通知,或者在索引exits时检查条件

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

"country" => isset($row['fields']['venue.country']) ? $row['fields']['venue.country'][0] : '',
//运行此代码进行测试

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);
错误报告(E_ALL);
ini设置(“显示错误”,1);
$jsondata=文件获取内容('https://hugo.events/genre/pop/next/200/100');
$data=json_decode($jsondata,true);
foreach($data[1]['hits']['hits']作为$row){
//echo$row['fields']['name'][0].=>。@$row['fields']['vention.country'][0].
; $array[]=数组( “name”=>$row['fields']['name'][0], “country”=>isset($row['fields']['vention.country'])?$row['fields']['vention.country'][0]:” ); } 回声“; 打印(数组);
您确定这是PHP错误吗?您得到了什么错误?输出是什么:
print\r($row)?仍然是相同的错误。我已将代码更改为与我的代码匹配如果您选择此路线,您还必须将:$row['fields']['venue.country'][0]更改为:$row['fields']['venuecountry'][0]如果您在更改后仍然收到相同的错误,则这是一个与字段名称中的点无关的错误,直到给出相同的错误为止。然后我必须检查API,它给了我很多非法的字符串偏移量:检查它在pop.php页面上使用的完整代码的前哨。这个错误毫无意义,除非你在“我不能使用a”中的字符串周围加上引号,这里你有一个完整的pop.php代码的幽灵箱这个php代码很好用,它基于你的ghostbin,删除了不相关的代码:当我使用你的代码时,我仍然无法在我的数据库中获取国家/地区。你的意思是你无法获取国家/地区,因为所有行都是少数行?我无法获取所有国家/地区的国家/地区。这很奇怪。我在答案中添加了一些代码,你能运行这些代码并检查输出中是否有国家。如果我尝试你添加的代码,它不会返回错误或任何东西,即使我添加了异常处理程序。