PHP 使用格式正确的 json 对象获得意外的 T_VARIABLE

PHP obtaining unexpected T_VARIABLE with well formed json object

我有这个格式良好的 JSON 项目(在网站 json 验证器上验证):

{"isNew":true,"creationDate":"2022/06/04","lastUpdate":"2022/06/04","IdStatus":2,"optionsObject":[{"id":"1","name":"XXXXXX","description":"XXXXXXXXXXXX","qty":"0","price":"0.00","label":"","placeholder":"","enabled":"1"},{"id":"6","name":"YYYYYY","description":"YYYYYYYYY","qty":"0","price":"0.00","label":"","placeholder":"","enabled":"1"},{"id":"7","name":"ZZZZZZZ","description":"ZZZZZZZZZZ","qty":"0","price":"0.00","label":"","placeholder":"","enabled":"1"}],"Id":"178","items":"4","startDate":"20/06/2022"}

并使用此代码在 PHP 中获取此对象:

$json_object = str_replace("\", "", $data);
$singleObject = json_decode( $json_object );

$sql = "INSERT INTO table1(field1, field2, field3, field4, field5, field6, field7) VALUE ("  .$singleObject->Id . ", '" . $singleObject->startDate ."', " . $singleObject->IdStatus . ", " . $singleObject->IdRoom . ", " . $singleObject->creationDate . ", " . $singleObject->lastUpdate . ", " . $singleObject->items . "')";

file_put_contents(plugin_dir_path( __FILE__ ) . '/testfile2.log', print_r( $sql . PHP_EOL, true), FILE_APPEND);

我可以在 testfile2.log 中看到我的 sql 句子格式正确,但如果我还添加此代码:

$optionsobject = json_decode($json_object, true);

foreach ($optionsobject->optionsObject as $optionobj) {
    $sql_options = "INSERT INTO table2 (field1, field2, field3, field4, field5) VALUES(" . $id . ", " . $optionobj->id . ", " 
                    $optionobj->qty .", " . $optionobj->price . ", '" . $optionobj->placeholder . "')";
                    
    file_put_contents(plugin_dir_path( __FILE__ ) . '/testfile.log', print_r( $sql_options . PHP_EOL, true), FILE_APPEND);
}

我得到这个错误:

[04-Jun-2022 09:48:33 UTC] PHP Parse error:  syntax error, unexpected '$optionobject' (T_VARIABLE) in...

当我知道通常要循环对象时,它会使用这个 foreach,正如我在几个示例中看到的那样...是否有可能我错过了这个 foreach 的细节?

在此先感谢大家! 干杯! :-)

你在 $optionobj->qt

之前少了一个点 .