第一种
这种场景比较少,大多数都是新手才会犯,也很容易发现解决错误。就是字面意思,将数组当成字符串使用了。
示例:
$arr = array(0, 1, 2);//错误1,双引号可以解析变量,但是双引号中,就会认为它是字符串。var_dump($arr);//错误2echo $arr;
这种情况会返回结果:
php notice: array to string conversion in /path/test.php on line 5
解决方案就是将数组转换成字符串再使用:比如使用json_encode($arr);
第二种
这种场景也比较少,而且只看提示 array to string conversion 很难理解哪里出错了。
在我们使用curl的时候,通过post传参数,当参数为二维数组的时候,会报这个错,这就很神奇了。
示例:
$data = array([0], [1], [3]);$ch = curl_init();curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_header, false);curl_setopt($ch, curlopt_returntransfer, 1);curl_setopt($ch, curlopt_post, 1);//报错行curl_setopt($ch, curlopt_postfields, $data);$result = curl_exec($ch);
这个问题解决方案也很简单。使用 http_build_query()处理一下参数就可以了。建议使用curl的时候,参数都用http_build_query处理一下。
curl_setopt($ch, curlopt_postfields, http_build_query($data));
推荐学习:php视频教程
以上就是php提示array怎么办的详细内容。
