Skip to content
Advertisement

I can’t generate the correct Json file in PHP

it does not work properly please help me to get the correct json file?

in particular when I inserted it in the site in php I got this array so the Json file creates it correctly

this is the first array I generate via submit of my form. this array I add it to the end of the Json file, but I get an unwanted result.

// CODE ---------------------------
  $_arr_POST    = array();
  $_arr_POST    = $_POST; // POST from FORM
  foreach ($_arr_POST as $key => $element) {
    $arr_index_POST[$key] = $element;
  }
  $data_path        = "path/file.json";
  $_GET_JSON        = file_get_contents($data_path);
  $_array_GET_Json  = json_decode($_GET_JSON,true);

if (count($_array_GET_Json) != 0 ){
    foreach ($_array_GET_Json as $key =>  $element) {
      $_array_element_GET = $element;
     }
     $_json_PUT[]   = array_merge ($_array_index_GET , $arr_index_POST);
  } else {
     $_json_PUT[]   = $arr_index_POST;
  }

file_put_contents($data_path, json_encode($_json_PUT));

get such a formatted array and that’s no good!

Array
(
    [0] => Array
        (
                [id] => 1
                [datetime] => 2021-08-05 05:11:34
                [product] => farfalle pink
                [code] => ff01pink
                [company_name] => Rome Capitale
                [status] => active
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 2
                                    [datetime] => 2021-08-05 05:11:34
                                    [product] => farfalle pink
                                    [code] => ff01pink
                                    [company_name] => Rome Capitale
                                    [status] => active
                                )

                            [id] => 3
                            [datetime] => 2021-08-05 05:11:34
                            [product] => farfalle pink
                            [code] => ff01pink
                            [company_name] => Rome Capitale
                            [status] => active
                        )
                [id] => 0
                [datetime] => 2021-08-05 05:11:34
                [product] => farfalle pink
                [code] => ff01pink
                [company_name] => Rome Capitale
                [status] => active
                )

        )

)

This is the result I have to get a simple json file like the one below

[
  {
    "id": "0",
    "datetime": "2021-08-03 05:00:34",
    "product": "farfalle pink",
    "code": "ff01pink",
    "company_name": "Rome Capitale",
    "status": "active"
  },
  {
    "id": "1",
    "datetime": "2021-08-04 05:11:34",
    "product": "farfalle blue",
    "code": "ff01blue",
    "company_name": "Rome Capitale",
    "status": "active"
  },
  {
    "id": "100",
    "datetime": "2021-08-05 05:11:34",
    "product": "farfalle white",
    "code": "ff01white",
    "company_name": "Rome Capitale",
    "status": "active"
  }
]

Advertisement

Answer

I’m not sure why you are over-complicating things in your code like that. If you just want to append the posted data you could use the following snippet

<?php
    $file = "path/file.json";

    if (!empty($_POST)) {
        $data = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
        if (!$data) $data = []; // Test if json_decode returned false or null (due to an invalid json string)
        $data[] = $_POST;
        file_put_contents($file, json_encode($data));
    }

I’d strongly suggest you to verify the submitted data first before saving/appending to the JSON file

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement