如何使用php将新值添加到嵌套的json文件中

时间:2018-02-20 05:32:40

标签: php arrays json

我是php的新手,但我想让php连接到一个json文件,以获取关于此处注册的人的简要信息,这是我目前为止所拥有的

if(isset($_SESSION['steamid'])){
    include  ('steamauth/userInfo.php');
    $steamid = $steamprofile['steamid'];
    $SteamName = $steamprofile['personaname'];
    // Read JSON file
        $object = file_get_contents('./steam/Accounts.json');
        //Decode JSON
        $json_data = json_decode($object);
    $filename = fopen("Users.json","a") or die("Unable to open file!");
    $obj = file_get_contents('Users.json');
    $jsonData = json_decode($obj);
    foreach($jsonData->Users as $item)
    {
        if ($item->$steamid != $steamid){
            $json_obj = array(
                "Users" => array(
                    $steamid => array(
                    "steamname" => $SteamName,
                    "Credits" => $Credits,
                    //"Items" => $Items,

                    ),
                ),
            );
            $myJSON = json_encode($json_obj, JSON_PRETTY_PRINT);
            fwrite($filename, $myJSON);
            fclose($filename);
        } else {

        }
    }
}

这是json文件

{
    "Users":{
        "steamid" :{
            "steamname":"Name",
            "Credits":0
         }
     }
 }

我想要做的是将新用户添加到Users对象下的json文件中,用户是$ steamid,但它说的是以下内容

Notice: Trying to get property 'Users' of non-object in C:\xampp\htdocs\Marketwh.com\index.php on line 18

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Marketwh.com\index.php on line 18

如果还有其他需要的信息,请告诉我

1 个答案:

答案 0 :(得分:0)

在将其转换为json之前,您需要构建一个正确的php多维数组。此外,无需对每次迭代进行编码和保存。

$json_obj = array();

//this is the new users you want to append to the json
$newUsers = array(array("steamname"=>"new user 1", "steamid"=> "3"),
                  array("steamname"=>"new user 2", "steamid"=> "4"),
                  array("steamname"=>"new user 3", "steamid"=> "1")
                 );


// this is the content of the saved json. you need to add the steamid as an index. 
// also recomanded, add the steamid as a leaf, makes it easier to access data
$json = '{
        "Users":{
            "1" :{
                "steamId":1, 
                "steamName":"UserName1",
                "credits":0
             },
            "2" :{
                "steamId":2, 
                "steamName":"UserName2",
                "credits":5
             }
         }
     }';

//convert json string to array
$jsonData = json_decode($json,true);//true flag d to convert to array    

// loop trough your new users to add
foreach($newUsers as $userToAdd) {
    $steamName = $userToAdd["steamname"];
    $steamId = $userToAdd["steamid"];

    // check if the id of the user to add is present in your saved data array
    if(!isset($jsonData["Users"][$steamId] )) {
        //if not, add it to the array
        $jsonData["Users"][$steamId] = array( 
            "steamid" => $steamId,
            "steamname" => $steamName,
            "credits" => 0 );
    } else {
        // if it is, do nothing. 
        // "new user 3" has steam id "1" that is already present in the saved json 
    }
}


$myJSON = json_encode($jsonData, JSON_PRETTY_PRINT);

print ($myJSON);