简体   繁体   English

json至php为foreach提供的无效参数

[英]json to php Invalid argument supplied for foreach

I have a Json file with data displayed below. 我有一个Json文件,其数据显示在下面。 im trying to insert into another file php code below to db. 我试图插入下面的另一个文件的PHP代码到数据库。 i keep getting ERROR: Invalid argument supplied for foreach and i do not understand why 我不断收到错误:为foreach提供了无效的参数,我不明白为什么

[{
  "DATE": "02-07-2017",
  "TIME": "08:20:48 AM",
  "ACCOUNT ID": "1000",
  "POSITION": "",
  "FIRST NAME": "",
  "LAST NAME": "",
  "ACCOUNT STATUS": "Active",``
  "REAL URL": ""
},{
  "DATE": "02-07-2017",
  "TIME": "08:20:49 AM",
  "ACCOUNT ID": "1000",
  "POSITION": "",
  "FIRST NAME": "",
  "LAST NAME": "",
  "ACCOUNT STATUS": "Active",
  "REAL URL": ""
},{
  "DATE": "02-07-2017",
  "TIME": "08:20:49 AM",
  "ACCOUNT ID": "1000",
  "POSITION": "Ceo",
  "FIRST NAME": "",
  "LAST NAME": "",
  "ACCOUNT STATUS": "Active",
  "REAL URL": ""
}]

$JSON_DATA = file_get_contents('Track.js');
$CLEAN_DATA = json_decode($JSON_DATA, true);
$stmt = $db->prepare("INSERT INTO `Track Employee` VALUES (?,?,?,?,?,?,?,?)");
foreach (($CLEAN_DATA) as $row) {
  $stmt->bindParam(1, $row['DATE']);
  $stmt->bindParam(2, $row['TIME']);
  $stmt->bindParam(3, $row['ACCOUNT ID']);
  $stmt->bindParam(4, $row['POSITION']);
  $stmt->bindParam(5, $row['FIRST NAME']);
  $stmt->bindParam(6, $row['LAST NAME']);
  $stmt->bindParam(7, $row['ACCOUNT STATUS']);
  $stmt->bindParam(8, $row['REAL URL']);
}

You have 2 problems with this JSON: 这个JSON有2个问题:

  1. JSON array should be wrapped with []. JSON数组应使用[]包装。

  2. You have unnecessary backticks on the 8th line. 您在第8行上有不必要的反引号。

    "ACCOUNT STATUS": "Active",`` “ ACCOUNT STATUS”:“有效”,``

When json_decode function gets an invalid JSON string, It returns FALSE, and since it is not an array - You're getting the foreach warning. json_decode函数获取无效的JSON字符串时,它将返回FALSE,并且由于它不是数组-您将收到foreach警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM