2017-10-18 72 views
0

當用戶編輯他/她自己的帳戶時,如果他/她的現有帳戶使用電子郵件應該能夠將其更新到phonenumber,反之亦然。如何在編輯帳戶時刪除JSON密鑰?

我以爲是檢查輸入的格式,如果輸入的是一個email和老一個是phonenumber然後刪除與phonenumber鑰匙,創建一個新的與email否則只是更新email關鍵的價值。和phonenumber一樣的故事。

下面的數組是我在users.txt文件中的用戶。

[ 

    { 
     "role": "admin", 
     "id": "59df4ef2d8d39", 
     "email": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "name": "E", 
     "lastname": "E", 
     "password": "5", 
     "image": "img_webshop\/userimage-59dfc460af866.png" 
    }, 
    { 
     "role": "user", 
     "id": "59e75231a393c", 
     "email": "[email protected]", 
     "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. 
    } 

    ?> 
+0

您是否將所有的userdetails保存在一個文本文件中?用純文本密碼? – Andreas

+0

它只是爲了假人而不是真正的項目。 – codeDragon

回答

1

如果你需要的,並在你的代碼在的意見要求:

//如何刪除RESPECTIVE PHONENUMBER KEY 和 //如何刪除RESPECTIVE EMAIL KEY

這可能是這麼簡單:

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

如果多數民衆贊成您正在尋找。

+0

這實際上是我所要求的,但我不能100%確定整體解決方案對於我想要做的事情在邏輯上是好的。你怎麼看? – codeDragon

+0

猜測完全取決於。我的意思是,如果你想刪除這些鍵,取消設置是一個很好的方法來將它們完全混合(去掉,finito)。但是如果你想保留鍵,但是讓它們爲空,那麼你不會使用unset,而是將它們分配給null。 – IncredibleHat

+0

它完成這項工作,謝謝。 – codeDragon

1

將truthy布爾參數傳遞給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; 
     } 
    } 
} 
+0

爲什麼你需要一個關聯數組呢?你能否解釋我,因爲我以前從未使用過它們。 – codeDragon

+0

我的不好。我的意思是一組數組。您已經使用關聯數組。 PHP數組是鍵值映射。 http://php.net/manual/en/language.types.array.php –

+1

這個答案中的重要部分是在循環中使用'引用',所以你可以通過該引用直接取消你不需要的密鑰。而另一種方式,你必須訪問原始數組變量與正確的索引和子鍵未設置(就像我的答案)。我個人更喜歡這個答案,因爲引用減少了內存開銷,並使代碼更簡潔。 – IncredibleHat