PHP脚本返回数组数组?

时间:2017-10-18 06:02:41

标签: php arrays json multidimensional-array file-put-contents

我创建了一个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);
}
    ?>

2 个答案:

答案 0 :(得分:0)

您的$json是一个多维数组,而函数file_put_contents()只接受字符串,数组或流作为数据内容。当数组是一维数组时,它存储数组的每个元素。当数组是多维数组时,它将导致

  

PHP注意:/home/xxxxx/test.php上的数组转换为字符串   第xx行

您可以测试简单的演示,它将ArrayArrayArray存储在aaa.txt文件中。而且数组&#39;是多维数组count([[1,2,3,4],[2],[3]])的长度为3。

<?php
file_put_contents("aaa.txt", [[1,2,3,4],[2],[3]]);

答案 1 :(得分:-1)

在写入文件之前检查print_r($ _ POST [&#39; OrderSummary&#39;])。必须是数组,