在MongoDb php驱动程序的变量中插入JSON
我将json文件转换为数组,但它在foreach无效参数中给了我错误在MongoDb php驱动程序的变量中插入JSON,php,mongodb,insert,Php,Mongodb,Insert,我将json文件转换为数组,但它在foreach无效参数中给了我错误 <?php require 'vendor/autoload.php'; $client = new MongoDB\Client; $test = $client->test; $createCollection = $test->restaurants; $string = file_get_contents("D:\Database\primer-dataset.json"); $json_a = j
<?php
require 'vendor/autoload.php';
$client = new MongoDB\Client;
$test = $client->test;
$createCollection = $test->restaurants;
$string = file_get_contents("D:\Database\primer-dataset.json");
$json_a = json_decode($string, true);
foreach ($json_a as $id => $item)
{
$createCollection->insert( $item);
?>
因为它说参数无效
,$string
可能不是有效的JSON。检查$string
和$json\u a
的内容以查看具体发生的情况。您是否检查了$json\u a
的值?是的,它是空的,我现在修复它,谢谢you@Pharaoh你应该把它作为一个答案,因为它似乎已经解决了萨福的问题。