Php 脚本返回数组数组?
Php script returning array array?
我创建了一个 php 脚本来读取从 android 应用程序发送到 php 的数据并将其保存在文件中,但是在打开文本文件时我得到以下声明:-
ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray
有人可以告诉我我哪里出错了,我该如何纠正它?
这是我的 php 脚本 :-
<?php
$password="";
$user="root";
$database="shadowpets";
$host="localhost";
$response=array();
$con=mysqli_connect($host,$user,$password,$database)or die("Unable to connect");
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(isset($_POST['OrderSummary']))
{
$data=$_POST['OrderSummary'];
$json=json_decode($data,true);
$file='text.txt';
$result=file_put_contents($file,$json);
$response["success"]=1;
$response["message"]="done";
}
else
{
$response["success"]=0;
$response["message"]="parameters not correctl formatted";
}
echo json_encode($response);
}
?>
在写入文件之前检查 print_r($_POST['OrderSummary'])。必须是数组,
你的$json
是一个多维数组,而函数file_put_contents()
只接受字符串、数组或流作为数据内容。当数组是一维数组时,它存储数组的每个元素。当数组为多维数组时,会导致,
PHP Notice: Array to string conversion in /home/xxxxx/test.php on
line xx
您可以测试简单的演示,它会将ArrayArrayArray
存储在aaa.txt文件中。而'Array'的个数就是多维数组的lenthcount([[1,2,3,4],[2],[3]])
也就是3.
<?php
file_put_contents("aaa.txt", [[1,2,3,4],[2],[3]]);
我创建了一个 php 脚本来读取从 android 应用程序发送到 php 的数据并将其保存在文件中,但是在打开文本文件时我得到以下声明:-
ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray
有人可以告诉我我哪里出错了,我该如何纠正它?
这是我的 php 脚本 :-
<?php
$password="";
$user="root";
$database="shadowpets";
$host="localhost";
$response=array();
$con=mysqli_connect($host,$user,$password,$database)or die("Unable to connect");
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(isset($_POST['OrderSummary']))
{
$data=$_POST['OrderSummary'];
$json=json_decode($data,true);
$file='text.txt';
$result=file_put_contents($file,$json);
$response["success"]=1;
$response["message"]="done";
}
else
{
$response["success"]=0;
$response["message"]="parameters not correctl formatted";
}
echo json_encode($response);
}
?>
在写入文件之前检查 print_r($_POST['OrderSummary'])。必须是数组,
你的$json
是一个多维数组,而函数file_put_contents()
只接受字符串、数组或流作为数据内容。当数组是一维数组时,它存储数组的每个元素。当数组为多维数组时,会导致,
PHP Notice: Array to string conversion in /home/xxxxx/test.php on line xx
您可以测试简单的演示,它会将ArrayArrayArray
存储在aaa.txt文件中。而'Array'的个数就是多维数组的lenthcount([[1,2,3,4],[2],[3]])
也就是3.
<?php
file_put_contents("aaa.txt", [[1,2,3,4],[2],[3]]);