I send the following data to my php file via ajax:
JSON.stringify({test1: '1', test2: '2'})
I would like to write this data to my JSON FILE content.json containing an empty array
[]
I only want to add the content though if it is not there, yet. This is my PHP code:
$jsonStringObject = file_get_contents("php://input");
$phpObject = json_decode($jsonStringObject);
$newJsonStringObject = json_encode($phpObject);
header('Content-Type: application/json');
$jsonString = file_get_contents('content.json');
$data = (array) json_decode($jsonString, true);
if (in_array($phpObject, $data) === false){
$data[] = $phpObject;
}
$newJsonString = json_encode($data);
file_put_contents('content.json', $newJsonString);
It almost works. Something is wrong with the way the data is added to the array because when I call the function for the first time, it updates content.json to
[null,{"test1":"1","test2":"2"}]
On calling the function again, it adds the object again despite the if-statement:
[null,{"test1":"1","test2":"2"},{"test1":"1","test2":"2"},{"test1":"1","test2":"2"}]
Can anyone help me to spot the mistake?
You're comparing apples and oranges (or rather objects to arrays).
Make sure you decode all json as arrays:
$jsonStringObject = file_get_contents("php://input");
// Added true as second argument to get it bas as an array
$phpObject = json_decode($jsonStringObject, true);
$jsonString = file_get_contents('content.json');
// Removed the (array) since the second argument literally means "return as array"
$data = json_decode($jsonString, true);
if (in_array($phpObject, $data) === false){
$data[] = $phpObject;
}
$newJsonString = json_encode($data);
file_put_contents('content.json', $newJsonString);
Demo: https://3v4l.org/rnCn5
I'm trying to convert a json file to csv but only the text "array,array" gets printed in the csv file. I'm guessing this is because multiple arrays get returned but as a noob i dont know how to fix this.
<?php
$jsonString = file_get_contents("data.json");
//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);
$jsonDecoded = $jsonDecoded;
//Give our CSV file a name.
$csvFileName = 'example.csv';
//Open file pointer.
$fp = fopen($csvFileName, 'w');
//Loop through the associative array.
foreach($jsonDecoded as $row){
//Write the row to the CSV file.
fputcsv($fp, $row);
}
//Finally, close the file pointer.
fclose($fp);
print $jsonDecoded;
echo json_last_error_msg();
?>
The json looks like this, contains multiple records and should be printed each on a row.
{"data":[{"ID":4,"UUID":"53F","A schematic overview of your activities":"Yes","Q1_1-1":"To some extent","Q1_1-2":"To some extent","Q1_1-3":"To some extent","Question 1_2":"Yes","Q1_2-1":"Yes","Q1_2-2":"To some extent","Q1_2-3":"No","Q1_2-4":"Yes","Q1_2-5":"To some extent","Q1_2-6":"Yes","Question 1_3":"Yes","Q1_3-1":"Yes","Q1_3-2":"To some extent","Q1_3-3":"To some extent","Q1_3-4":"No","Q1_3_5":"Yes","Question 2":"To some extent","Q2_2":"To some extent","Q2_3":"To some extent","Q2_4":"To some extent","Question 3":"No","Q3_2":"No","Q3_3":"To some extent","Q3_4":"Yes","Question 3_2":"Yes","Q3_2-2":"Yes","Q3_2-3":"To some extent","Question 3_3":"No","Q3_3-2":"To some extent","Q3_3-3":"Yes","Q3_3-4":"To some extent","Question 3_4":"Yes","Q3_4-2":"To some extent","Q3_4-3":"To some extent","Q3_4-4":"To some extent","Question 3_5":"Yes","Q3_5-2":"To some extent","Q3_5-3":"Yes","Q3_5-4":"Yes","Q3_5_5":"To some extent","Q3_5-6":"To some extent","Question 3_6":"Yes","Q3_6-2":"Yes","CreatedAt":"2019-08-14T10:38:07.033Z","CreatedBy":"qqq","UpdatedAt":null,"UpdatedBy":null,"CreatedByID":20,"UpdatedByID":null},{"ID":5,"UUID":"2D40","A schematic overview of your activities":"Yes","Q1_1-1":"To some extent","Q1_1-2":"To some extent","Q1_1-3":"Yes","Question 1_2":"Yes","Q1_2-1":"To some extent","Q1_2-2":"No","Q1_2-3":"To some extent","Q1_2-4":"Yes","Q1_2-5":"Yes","Q1_2-6":"To some extent","Question 1_3":null,"Q1_3-1":null,"Q1_3-2":null,"Q1_3-3":null,"Q1_3-4":null,"Q1_3_5":null,"Question 2":null,"Q2_2":null,"Q2_3":null,"Q2_4":null,"Question 3":"No","Q3_2":"To some extent","Q3_3":"To some extent","Q3_4":"To some extent","Question 3_2":"Yes","Q3_2-2":"To some extent","Q3_2-3":"Yes","Question 3_3":"Yes","Q3_3-2":"No","Q3_3-3":"To some extent","Q3_3-4":"Yes","Question 3_4":"Yes","Q3_4-2":"To some extent","Q3_4-3":"Yes","Q3_4-4":"Yes","Question 3_5":"No","Q3_5-2":"To some extent","Q3_5-3":"To some extent","Q3_5-4":"Yes","Q3_5_5":"To some extent","Q3_5-6":"To some extent","Question 3_6":"Yes","Q3_6-2":"To some extent","CreatedAt":"2019-08-19T13:48:22.770Z","CreatedBy":"qqq","UpdatedAt":null,"UpdatedBy":null,"CreatedByID":20,"UpdatedByID":null}]}
You just need to refer to the 'data' key.
foreach($jsonDecoded['data'] as $row){ ...
https://www.php.net/manual/en/splfileobject.fputcsv.php
$jsonString = file_get_contents("data.json");
//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);
$list= $jsonDecoded;
file = new SplFileObject('example.csv', 'w');
foreach ($list as $fields) {
$file->fputcsv($fields);
}
Can anyone please shows me how to get the input of PHP to JSON file (.json) and read data from JSON file and display in PHP (Echo).
for example:
$myObj->name = "John";
$myObj->age= 20;
to result.json
{"name":"John","Age":20}
and retrieve from result.json and display data in PHP as
name=John
Age=20
To convert the object to json use this:
$json = json_encode($myObj);
See the json_encode docs.
To return it back to the format you want try this...
$obj = json_decode($json);
$name = $obj->name; // John
$age = $obj->age; // 20
See the json_decode docs.
To iterate over keys and values do something like this:
foreach($obj as $key=>$value)
{
echo $key . " = " . $value . "\n";
}
json_encode() is used for encoding PHP data into a JSON format and json_decode() is used to decode JSON into a PHP data
json_encode documentation:
http://php.net/manual/en/function.json-encode.php
json_decode documentation
http://php.net/manual/en/function.json-decode.php
if you are having an php array you can convert it into json.
$json_string = json_encode($array);
and write this into a json file.
$fp = fopen('results.json', 'w');
fwrite($fp, json_string);
fclose($fp);
now convert your json string which is in results.json to array.
$str = file_get_contents('./results.json');
$array = json_decode($str, true); // decode the JSON into an associative array
I want to know how can I do to convert neo4j database to json file using PHP.
I used the code below but i want to know the similar of mysqli_fetch_assoc in Neo4j:
$req= "match n return n";
$result = $client->sendCypherQuery($req)->getResult(); //create an array
$emparray[] = array();
while($row =mysqli_fetch_assoc($result)) { $emparray[] = $row; }
echo json_encode($emparray); //write to json file
$fp = fopen('data/charlize.json', 'w');
fwrite($fp, json_encode($emparray));
fclose($fp);
to get all of your database as a JSON you can simply send a post request to http://<your database ip>/db/data/cypher with param "query" : "Match a return a" this will return a JSON string as response, then you can do whatever you want (print it in a file for example).
I want to retrieve array value.
This is my array value:
overlay.txt:
{"title":"sss","description":"sss","code":"sss"}
{"title":"trtr","description":"trtr","code":"tyrytr"}
{"title":"ret54","description":"56tr","code":"ty76"}
{"title":"rgfdg","description":"dfgdfg","code":"dfgdfg"}
{"title":"asfafdsf","description":"sdfsdf","code":"sdfsdfsdf"}
This is my code: but this is not working.why?
How to retrieve value from overlay.txt file.
I did not get all title value.
I do not known how to get title value from overlay.txt
The $title is showing empty.
Where I want to change in my code to get $title value.
$info = array();
$folder_name = $this->input->post('folder_name');
$info['title'] = $this->input->post('title');
$info['description'] = $this->input->post('description');
$info['code'] = $this->input->post('code');
$json = json_encode($info);
$file = "./videos/overlay.txt";
$fd = fopen($file, "a"); // a for append, append text to file
fwrite($fd, $json);
fclose($fd);
$filecon = file_get_contents('./videos/overlay.txt', true);
$this->load->view('includes/overlays',$filecon);
//overlays page;
foreach($filecon as $files)
{
$title=$files['title'];
echo $title;
}
You're encoding your array to JSON, so at some point you need to decode it again into a PHP array. Since you actually have several JSON objects in the file, you need to decode each one individually. Assuming it's always one JSON object per line, this'll do:
$jsonObjects = file('overlay.txt', FILE_IGNORE_NEW_LINES);
foreach ($jsonObjects as $json) {
$array = json_decode($json, true);
echo $array['title'];
...
}
This will very quickly break if there are line breaks within the serialized JSON, e.g.:
{"title":"ret54","description":"foo
bar","code":"ty76"}
That way of storing the data is not very reliable.
make overlay.txt fully json format:
[
{"title":"sss","description":"sss","code":"sss"},
{"title":"trtr","description":"trtr","code":"tyrytr"},
...
]
and try this:
$raw = file_get_contents('./videos/overlay.txt', true);
$this->load->view('includes/overlays', array("filecon" => json_decode($raw)));
overlay page:
<?php
foreach($filecon as $files) {
echo $files['title'];
}
?>
If you want to use $filecon in view file,
set an array which has the key "filecon" in $this->load->view()'s second argument.
http://codeigniter.com/user_guide/general/views.html