[英]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?
I made a signup where you can signup with either email or phone number, and the system will only store either.我做了一个注册,您可以使用电子邮件或电话号码注册,系统只会存储其中之一。 The way I do it:我这样做的方式:
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
}
The functions which detects the different formats:检测不同格式的函数:
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.
}
So my text file will look the following:所以我的文本文件将如下所示:
[
{
"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"
},
]
Now my question is when I am editing the account how can I grab the email or phonenumber key as one and assign a new value to it ( again it can be an email or a phonenumber? For example $ajUsers[$i]-> ? = $sNewUserEmailorPhoneNumber;
I hope it´s understandable what I want.现在我的问题是,当我编辑帐户时,如何将电子邮件或电话号码作为一个键并为其分配一个新值(同样可以是电子邮件或电话号码?例如$ajUsers[$i]-> ? = $sNewUserEmailorPhoneNumber;
我希望我想要的是可以理解的。
first of all, I advise you to refactor your code, you should not be using the POST data in your format check functions首先,我建议你重构你的代码,你不应该在你的格式检查函数中使用 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
}
even better you would have two methods on your object something like:更好的是,您的对象上会有两种方法,例如:
public function setIdentifyingAttribute($emailOrPhone){....}
public function getIdentifyingAttribute(){....}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.