I have this well formed JSON oject (verified on website json verificator):
{"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"}
and obtaining this object in PHP with this code:
$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);
I can see in testfile2.log I have well formed sql sentence, but if I add also this code:
$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);
}
I obtain this error :
[04-Jun-2022 09:48:33 UTC] PHP Parse error: syntax error, unexpected '$optionobject' (T_VARIABLE) in...
when I know usually to loop object it's used this foreach as I can see in several examples... is there maybe a detail I missed with this foreach?
Thanks in advance to all! Cheers! :-)