php - Cannot use object of type stdClass as array - JSON -


this question has answer here:

i'm working .json files , have following function:

//rights array (write, create, delete, addusers, delete users) function adduser($requester, $username, $rights) {     $file = $requester->getfolder() . "projects/" . $this->name . "/data/users.json";     $json = json_decode(file_get_contents($file, false));     $json[$username] = array("write" => $rights[0], "create" => $rights[1], "delete" => $rights[2], "adduser" => $rights[3], "deleteuser" => $rights[4]);     file_put_contents($file, json_encode($json)); }    

whenever run code uses function, following error:

fatal error: cannot use object of type stdclass array in...

the second parameter of json_decode defaults false, meaning produce object. use true produce array.

the error comes $json[$username] accessing $json array. when object, access like: $json->$username.