I have updated your code and tested at my side it's working now. Please try it.
<?php
include 'Config.php';
if(!empty($_GET['params'])){
$getJSON = $_GET['params'];
echo 'getJSON: '.$getJSON.'<br><br>';
$updateArr = json_decode($getJSON, true);
echo 'UPDATE_ARR: ';print_r($updateArr); //<-- it prints 'null'...
$jsonStr = file_get_contents("Users.json");
// Decode the JSON string into a PHP array.
$objects = json_decode($jsonStr, true);
array_push($objects, $updateArr);
// Encode the array back into a JSON string and save it.
$jsonData = json_encode($objects);
file_put_contents("Users.json", $jsonData);
// echo data
echo 'JSON DATA: ' .$jsonData;
}else{
echo 'Params is empty';
}
This is the outcome of users.json file after refreshing many times:
getJSON: {"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]}
UPDATE_ARR: Array ( [objID] => i8O0FRuGEr [username] => johndoe [password] => 123456 [email] => [email protected] [followedBy] => Array ( [0] => john [1] => sarah ) ) JSON DATA: {"ID":"i8O0FRuGEr","user_name":"johndoe","pass":123456,"email_id":"[email protected]","followed_By":["john","sarah"],"0":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"1":null,"2":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"3":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"4":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"5":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"6":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"7":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"8":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]}}
After using your users.json file:
[{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]}]