簡體   English   中英

如何使用 PHP 訪問 JSON 對象的鍵,該鍵是數組中同一位置的電子郵件或電話號碼?

[英]How can I access with PHP a key of JSON objects which is either email or phone number at the same position in an array?

我做了一個注冊,您可以使用電子郵件或電話號碼注冊,系統只會存儲其中之一。 我這樣做的方式:

if ( fnCheckEmailFormat ( $sNewUserEmail ) ) {          // call the function which checks if is a valid email
     $jNewUser->email = $_POST['txtEmailorPhoneNumber']; // then assign an email key to the object user
} 
else if ( fnCheckDigitFormat ( $sNewUserPhoneNumber ) ) {  // call the function which checks that it should only contain                                                                     digits
        $jNewUser->phonenumber = $_POST['txtEmailorPhoneNumber']; // then assign a phonenumber key to the object user
}

檢測不同格式的函數:

function fnCheckEmailFormat ( $sNewUserEmail ){ //checks if the email is a valid email format
    $sNewUserEmail = $_POST['txtEmailorPhoneNumber'];
    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 phonenumber it conatains only digits
    $sNewUserPhoneNumber =  $_POST['txtEmailorPhoneNumber'];
    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.
}

所以我的文本文件將如下所示:

[
    {
        "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"
    },

]

現在我的問題是,當我編輯帳戶時,如何將電子郵件或電話號碼作為一個鍵並為其分配一個新值(同樣可以是電子郵件或電話號碼?例如$ajUsers[$i]-> ? = $sNewUserEmailorPhoneNumber;我希望我想要的是可以理解的。

首先,我建議你重構你的代碼,你不應該在你的格式檢查函數中使用 POST 數據

if ( fnCheckEmailFormat ( $_POST['txtEmailorPhoneNumber'] ) ) {          // call the function which checks if is a valid email
     $jNewUser->email = $_POST['txtEmailorPhoneNumber']; // then assign an email key to the object user
} 
else if ( fnCheckDigitFormat ( $_POST['txtEmailorPhoneNumber'] ) ) {  // call the function which checks that it should only contain                                                                     digits
        $jNewUser->phonenumber = $_POST['txtEmailorPhoneNumber']; // then assign a phonenumber key to the object user
}

更好的是,您的對象上會有兩種方法,例如:

public function setIdentifyingAttribute($emailOrPhone){....}
public function getIdentifyingAttribute(){....}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM