Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中迭代json数组_Php_Arrays_Json - Fatal编程技术网

如何在php中迭代json数组

如何在php中迭代json数组,php,arrays,json,Php,Arrays,Json,在我的纸条中,我从POST参数读取json数组,如下所示$data=$\u POST[“data”]。我想把这个数组中的每个对象都附加到txt文件中 我该怎么做 我的代码不起作用,我不知道为什么 $data = json_decode($data, true); foreach ($data as $v) { $myfile = file_put_contents('logs.txt', implode(" ", $v) . PHP_EOL, FILE_APPE

在我的纸条中,我从POST参数读取json数组,如下所示
$data=$\u POST[“data”]。我想把这个数组中的每个对象都附加到txt文件中

我该怎么做

我的代码不起作用,我不知道为什么

    $data = json_decode($data, true);

    foreach ($data as $v) {
        $myfile = file_put_contents('logs.txt', implode(" ", $v) . PHP_EOL, FILE_APPEND | LOCK_EX);
    }

通过输入
[{a:{b:[]},{c:'foo'}]
我得到了为foreach提供的无效参数。

打印
$myfile
的输出,以查看哪些
文件\u put\u contents()
返回的是无效的json。你需要弄清楚你提交的那篇json的帖子,因为它不会按原样解码。“应为带引号的对象属性名”,带有
[{“a”:{“b”:[]},{“c”:“foo”}]
-错误-数组到字符串conversion@vehumu
内爆()
无法与
{a:{b:[]}
一起使用。
infrade()
的参数必须是一个包含字符串和/或数字的数组,但此数组的元素是另一个数组。您希望日志消息是什么样子的?