使用 PHP 从文本文件中仅提取电子邮件和密码
Extract only email and password from text file using PHP
我有一个数组,其中包含我可以说的字符串类型的对象
Array
(
[0] => Name=xyz
[1] => Email=xyz43@gmail.com
[2] => Password=xyz@123
[3] => Address=xyz University Lucknow
[4] => City=xyz
[5] => Name=peter
[6] => Email=peter43@gmail.com
[7] => Password=peter@123
[8] => Address=address
[9] => City=bla
[10] => Name=Jack
[11] => Email=jack76@gmail.com
[12] => Password=jack123
[13] => Address=jackAddress
[14] => City=jackCity
)
期望输出
Array
(
[Email] => xyz43@gmail.com
[Email] => peter43@gmail.com
[Email] => jack76@gmail.com
[Password] =>xyz@123
[Password] =>peter@123
[Password] =>jack123
)
坦白说,我想要这些东西用于登录,在此先感谢您。
你可以试试this
$stringArray = ['Name=xyz','Email=xyz43@gmail.com','Password=xyz@123','Address=xyz University Lucknow','Name=xyz','Email=xyz413@gmail.com','Password=xyz1@123','Address=xyz University Lucknow'];
$loginCredentials = [];
$i = 0;
foreach($stringArray as $item){
$data = explode('=', $item);
if(in_array('Email',$data) || in_array('Password',$data)){
$loginCredentials[$i][$data[0]] = $data[1];
if(isset($loginCredentials[$i]['Email']) && isset($loginCredentials[$i]['Password'])){
$i++;
}
}
}
//Output desired array
print_r($loginCredentials);
我有一个数组,其中包含我可以说的字符串类型的对象
Array
(
[0] => Name=xyz
[1] => Email=xyz43@gmail.com
[2] => Password=xyz@123
[3] => Address=xyz University Lucknow
[4] => City=xyz
[5] => Name=peter
[6] => Email=peter43@gmail.com
[7] => Password=peter@123
[8] => Address=address
[9] => City=bla
[10] => Name=Jack
[11] => Email=jack76@gmail.com
[12] => Password=jack123
[13] => Address=jackAddress
[14] => City=jackCity
)
期望输出
Array
(
[Email] => xyz43@gmail.com
[Email] => peter43@gmail.com
[Email] => jack76@gmail.com
[Password] =>xyz@123
[Password] =>peter@123
[Password] =>jack123
)
坦白说,我想要这些东西用于登录,在此先感谢您。
你可以试试this
$stringArray = ['Name=xyz','Email=xyz43@gmail.com','Password=xyz@123','Address=xyz University Lucknow','Name=xyz','Email=xyz413@gmail.com','Password=xyz1@123','Address=xyz University Lucknow'];
$loginCredentials = [];
$i = 0;
foreach($stringArray as $item){
$data = explode('=', $item);
if(in_array('Email',$data) || in_array('Password',$data)){
$loginCredentials[$i][$data[0]] = $data[1];
if(isset($loginCredentials[$i]['Email']) && isset($loginCredentials[$i]['Password'])){
$i++;
}
}
}
//Output desired array
print_r($loginCredentials);