如何在编辑帐户时删除JSON密钥?

时间:2017-10-18 18:24:55

标签: php json

当用户编辑自己的帐户时,如果他/她的现有帐户使用电子邮件,则应该能够将其更新为phonenumber,反之亦然。

我的想法是检查输入的格式,输入是email而旧的是phonenumber,然后用phonenumber删除密钥并创建使用email else的新内容只会更新email密钥的值。与phonenumber相同的故事。

以下数组是users.txt文件中的用户。

   [

    {
        "role": "admin",
        "id": "59df4ef2d8d39",
        "email": "a@a.dk",
        "name": "A",
        "lastname": "A",
        "password": "1",
        "image": "img_webshop\/userimage-59dfb91515810.png"
    },
    {
        "role": "user",
        "id": "59df4f1b070e6",
        "phonenumber": "12345678",
        "name": "B",
        "lastname": "B",
        "password": "2",
        "image": "img_webshop\/userimage-59e37de69475b.png"
    },
    {
        "role": "user",
        "id": "59dfc0cb07985",
        "email": "c@c.dk",
        "name": "C",
        "lastname": "C",
        "password": "3",
        "image": "img_webshop\/userimage-59dfc0cb06c5f.png"
    },
    {
        "role": "user",
        "id": "59dfc22f26f78",
        "phonenumber": "87654321",
        "name": "D",
        "lastname": "D",
        "password": "4",
        "image": "img_webshop\/userimage-59dfc22f2638d.png"
    },
    {
        "role": "user",
        "id": "59dfc460b261e",
        "email": "e@e.dk",
        "name": "E",
        "lastname": "E",
        "password": "5",
        "image": "img_webshop\/userimage-59dfc460af866.png"
    },
    {
        "role": "user",
        "id": "59e75231a393c",
        "email": "x@x.dk",
        "name": "Y",
        "lastname": "Y",
        "password": "",
        "image": "img_webshop\/userimage-59e79184d0335."
    }
]

这是处理更新的PHP API。

  //GETTING FROM FILE:
    $sajUsers = file_get_contents('users.txt');
    $ajUsers = json_decode($sajUsers);

    //_________________________________________________________//


    // getting it from the front end:
    $sUserId = $_POST['txtUpdateUserId'];
    $sNewUserRole = $_POST['txtUpdateUserRole'];
    $sNewUserName = $_POST['txtUpdateUserName'];
    $sNewUserLastName = $_POST['txtUpdateUserLastName'];
    $sNewUserEmailorPhoneNumber = $_POST['txtUpdateUserEmailorPhoneNumber'];
    $sNewUserPassword = $_POST['txtUpdateUserPassword'];

    //_________________________________________________________//


    $match_found = false;
    //The is getting it from the database.
    for ( $i = 0; $i < count( $ajUsers ); $i++ ) {  
         if ( $sUserId  == $ajUsers[$i]->id ) { //checks if the value of the username is equal to the value in the array.
            $ajUsers[$i]->role = $sNewUserRole;
            if ( fnCheckEmailFormat ( $sNewUserEmail ) ) {          // call the function which checks if is a valid email
                 if ( $ajUsers[$i]->phonenumber ) {

                     //HOW TO DELETE THE RESPECTIVE PHONENUMBER KEY 

                     $ajUsers[$i]->email = $sNewUserEmailorPhoneNumber; //AND INSTEAD ASSIGN AN EMAIL KEY
                 }

                 else {

                     $ajUsers[$i]->email = $sNewUserEmailorPhoneNumber;
                 }
            } 
              else if ( fnCheckDigitFormat ( $sNewUserPhoneNumber ) ) {  // call the function which checks that it should only                                                                     contain digits
                 if ( $ajUsers[$i]->email ) {

                           //HOW TO DELETE THE RESPECTIVE EMAIL KEY
                      $ajUsers[$i]->phonenumber = $sNewUserEmailorPhoneNumber;

                     }      
                 else {

                     $ajUsers[$i]->phonenumber = $sNewUserEmailorPhoneNumber;
                 }
            }
            $ajUsers[$i]->name = $sNewUserName;
            $ajUsers[$i]->lastname = $sNewUserLastName;
            $ajUsers[$i]->password = $sNewUserPassword;
            $ajUsers[$i]->image = $sNewUserImageUrl;
            $match_found = true;
            break;
        } 
    }

    //_________________________________________________________//


    if( $match_found ) {

        //PUTTING TO FILE:
        $sajNewUsers = json_encode( $ajUsers, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE  );
        file_put_contents('users.txt', $sajNewUsers);

        echo $sjResponse = '{"update":"ok"}';
        exit; //end the if statement and exit if it works.
    }

    else {
            echo $sjResponse = '{"update":"error"}'; // it didnt work.
            exit;
         }

    //_________________________________________________________//


    function fnCheckEmailFormat ( $sNewUserEmail ){ //checks if the property is valid. Called in line 6.
        $sNewUserEmail = $sNewUserEmailorPhoneNumber;
        if ( !filter_var( $sNewUserEmail, FILTER_VALIDATE_EMAIL ) ){
            return false; // returns false if its not valid. Then it wont run the if.
        }
        return true; // else it will run the signin.
    }

    function fnCheckDigitFormat ( $sNewUserPhoneNumber ){ //checks if the property is valid. Called in line 6.
        $sNewUserPhoneNumber = $sNewUserEmailorPhoneNumber;
        if ( !preg_match( "/^[0-99]+$/", $sNewUserPhoneNumber ) ){
            return false; // returns false if its not valid. Then it wont run the if.
        }
        return true; // else it will run the signin.
    }

    ?>

2 个答案:

答案 0 :(得分:1)

如果你需要,并且在你的代码中要求:

  

//如何删除相应的PHONENUMBER KEY   和   //如何删除相应的电子邮件密钥

可能很简单:

unset($ajUsers[$i]->phonenumber);
// and 
unset($ajUsers[$i]->email);
// respectively

如果那就是你要找的东西。

答案 1 :(得分:1)

通过将truthy boolean参数传递给json_decode,将$sajUsers解码为关联数组数组。

$ajUsers = json_decode($sajUsers, true);

然后循环$ajUsers,确保指向$user的指针是一个引用,前缀为&符号。

foreach($ajUsers as $key=>&$user)
{
    if($user['id'] == $sUserId) 
    {
        $match_found = true;

        if(fnCheckEmailFormat($sNewUserEmailorPhoneNumber))
        {
            $user['email'] = $sNewUserEmailorPhoneNumber;
            unset($user['phonenumber']);
            continue;
        }

        if(fnCheckDigitFormat($sNewUserEmailorPhoneNumber))
        {
            $user['phonenumber'] = $sNewUserEmailorPhoneNumber;
            unset($user['email']);
            continue;
        }
    }
}