-1

I have created an php script in order to read data send from an android app to php and save it in file but upon opening the text file i'm getting following statement :-

ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

instead of data could somebody tell me where iam going wrong and how can i rectify it ?

Here is my php script :-

<?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);
}
    ?>
LF00
  • 27,015
  • 29
  • 156
  • 295
atul kumar
  • 55
  • 6

2 Answers2

0

Your $json is an multidimension array, while function file_put_contents() only accept a string, array, or stream as the data content. When the array is one dimension array, it store each element of the array. When the array is multidimension array, it will cause,

PHP Notice: Array to string conversion in /home/xxxxx/test.php on line xx

You can test simple demo, which will store ArrayArrayArray in the aaa.txt file. And the number of 'Array' is lenth of the multidimension array count([[1,2,3,4],[2],[3]]) which is 3.

<?php
file_put_contents("aaa.txt", [[1,2,3,4],[2],[3]]);
LF00
  • 27,015
  • 29
  • 156
  • 295
  • the problem is solved i want to know how can i read it's element and store it in the database – atul kumar Oct 18 '17 at 06:58
  • This answer can't be true as OP does `file_put_contents($file, json_decode($_POST['OrderSummary'], true));`. If `$_POST['OrderSummary']` was indeed a (multidimensional) array the output in his file would empty as [`json_decode([[],], true)`](https://ideone.com/ki9o12) returns `NULL` – DarkBee Oct 18 '17 at 07:17
  • @DarkBee Here I make a mistake, I just want to say OP use a multidimension array as the parameter of `file_put_contents` which cause the issue. – LF00 Oct 18 '17 at 07:23
  • @KrisRoofe True, but storing a multidimensional array in a file will only output one `Array` in it. OP is prolly using `FILE_APPEND` in his/her original code. Nonetheless OP ain't clear on what he/she want. – DarkBee Oct 18 '17 at 07:30
  • @DarkBee You can test the demo in the answer. – LF00 Oct 18 '17 at 07:31
  • @DarkBee the number of `Array` is the first dimension. In you demo code, the first dimension of your array is 1. – LF00 Oct 18 '17 at 07:43
  • @KrisRoofe Point taken there – DarkBee Oct 18 '17 at 07:49
-1

Check for print_r($_POST['OrderSummary']) before writing into file. Must be array,

Blesson Christy
  • 380
  • 3
  • 13