2017-10-18 17 views
0

ユーザーが自分のアカウントを編集するときに、既存のアカウントが電子メールの場合は、phonenumberに更新する必要があります。逆の場合も同様です。アカウントを編集する際にJSONキーを削除するにはどうすればよいですか?

私は何を思ったことは入力のフォーマットのために、入力がemailされ、古いものはphonenumberはその後、ちょうどemailキーを更新phonenumberでキーを削除し、他の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

あなたのすべてのユーザー詳細をテキストファイルに保存しますか?パスワードはプレーンテキストで? – Andreas

+0

ダミーのためのものではなく、実際のプロジェクトです。 – codeDragon

答えて

1

あなたが必要とするすべて、とのコメントであなたのコード内を求めている場合は、次

各々のPHONENUMBER KEY を削除する方法を

//それぞれの電子メールを削除する方法 //

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

KEYそれはのような単純なものも

あなたが探していたことがあれば。

+0

これは実際に私が求めているものですが、全体的な解決策が私がしたいことに対して論理的に良いと100%確信しているわけではありません。どう思いますか? – codeDragon

+0

それは完全に依存すると思います。つまり、それらのキーを削除したい場合、unsetはそれらを完全に入れ替える良い方法です(消えて、finito)。しかし、キーを保持したいが空である場合は、unsetを使用せず、むしろそれらをnullに割り当てます。 – IncredibleHat

+0

これで仕事をします、ありがとうございます。 – codeDragon

1

$sajUsersを真理値のブール引数をjson_decodeに渡すことによって連想配列の配列にデコードします。そして

$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

関連する問題