2

I have the following data from an api call:

$userId = 1234; and $accessToken = 6789;. I want to write this to a json file called users.json.

Following this I make another api call and receive the following data:

$userId = 5678; and $accessToken = 0123;.

I then want to add this to the json file so that it looks like this:

[
    {
        "userId": "1234",
        "accessToken": "6789"
    },
    {
        "userId": "5678",
        "accessToken": "0123"
    },
]

My code for writing to the json file looks like this. The $userId and $accessToken are defined elsewhere and are returning the correct values:

$content = file_get_contents('users.json');
$tempArray = json_decode($content, true);
print_r($tempArray); // this doesn;t show anything as the users.json file contains `null`

array_push($tempArray, $userId);
array_push($tempArray, $accessToken);
$jsonData = json_encode($tempArray);
file_put_contents('users.json', $jsonData);

Unfortunately this isn't working. When I view the json file it just contains null Can anyone see any error with my code?

Thanks Raul

3
  • can you post this output here $tempArray = json_decode($content, true); print_r($tempArray); we want know how your json look before updating the value . Commented Jun 23, 2017 at 8:51
  • sure, i will add it to the question, still null though Commented Jun 23, 2017 at 8:53
  • @RaulRodriguez check my answer below Commented Jun 23, 2017 at 9:04

3 Answers 3

1

Your code is wrong. Correct it to look like this

 <?php
     $content = file_get_contents('users.json');
     $tempArray = json_decode($content, true);
     if(empty($tempArray)){
       $tempArray = [];
     }
     $newData = [
        "userId" => $userId,
        "accessToken" => $accessToken
     ];
     array_push($tempArray, $newData);
     $jsonData = json_encode($tempArray);
     file_put_contents('users.json', $jsonData);  

This will definitely work.

Sign up to request clarification or add additional context in comments.

1 Comment

@CBroe When you encode the array, it will convert associative array to object so ['x' => 'y'] will become {"x" : "y"}. When you decode an object (in php, the object is converted to an associative array (if 2nd arg to decode function is true) else a Stdclass object (if its false). What i did here is covert the json string to an array of associative arrays. So the above code will definitely work. Cheers :)
0

You need to to json_decode($content, true); to obtain an array and not an object.

See http://php.net/manual/fr/function.json-decode.php

Also the array_push is not correctly called.

It should be : array_push($tempArray, ["userid"=>$userId,"accesstoken" => $accessToken]);

5 Comments

@RaulRodriguez your array_push should be : array_push($tempArray, ["userid"=>$userId,"accesstoken" => $accessToken]);
@RaulRodriguez Try to put [] into the users.json file before starting to obtain an empty array and not null
ah ok. The answer above works by WdyDev. how does that code look to you?
@Esteban it works when i put an empty array in there first
@RaulRodriguez It does the same than what I did, just that I told you to put [] into the file to avoid getting null, in his code he tests that the value you obtain is not null and create the empty array. Basically the same problem we pointed out
0

You have error in json file so that all suggesion not working remove , at end of array. Then try below code :

Your json file should be:

[
    {
        "userId": "1234",
        "accessToken": "6789"
    },
    {
        "userId": "5678",
        "accessToken": "0123"
    }
]

php :

$content = file_get_contents('user.json');
$tempArray = json_decode($content,true);
$new_array = array("userId"=>$userId,"accesstoken"=>$accessToken);
array_push($tempArray, $new_array);
$jsonData = json_encode($tempArray);
file_put_contents('user.json', $jsonData);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.