2015-09-28 26 views
-2

我有一个数组。该数组分配给一个变量,但我无法读取该数组中的电子邮件。该数组在下面给出。我无法读取阵列形式的唯一电子邮件

$users = array(3) { [0]=> object(stdClass)#404 (4) { ["guid"]=> string(1) "2" ["name"]=> string(13) "xyz" ["username"]=> string(13) "xyz" ["email"]=> string(23) "[email protected]" } [1]=> object(stdClass)#405 (4) { ["guid"]=> string(3) "138" ["name"]=> string(12) "wxyz" ["username"]=> string(5) "wxyz" ["email"]=> string(21) "[email protected]" } [2]=> object(stdClass)#406 (4) { ["guid"]=> string(3) "126" ["name"]=> string(13) "xxxx" ["username"]=> string(7) "xxxx" ["email"]=> string(17) "[email protected]" } } 

我需要输出中那样

array(3) { [0]=> string(22) "[email protected]" [1]=> string(19) "[email protected]" [3]=> string(19) "[email protected]"} 

请给我reply.Advance非常感谢你。

+1

谷歌:'PHP array_map()' – Rizier123

+0

谢谢合租多先生,给了对上述问题的信息。我有类似的代码 foreach($ users as $ user) \t { \t \t $ to_name = $ user-> name; \t \t如果($用户>名== '') \t \t { \t \t $ TO_NAME = $用户>用户名; \t \t} \t $ to_email = $ user-> email; \t \t} – sreedhar

+0

如何以类似数组的形式读取电子邮件(3){[0] => string(22)“[email protected]”[1] => string(19)“wxyz @ gmail .com“[3] => string(19)”[email protected]“}。在上面的代码中。 – sreedhar

回答

0

你可以简单地使用array_walk功能在这里等作为

$result = []; 
array_walk($users,function($v,$k)use(&$result){ 
    $result[$k] = $v->email; 
}); 

使用array_map

$result = array_map(function($v){return $v->email;},$arr); 
0

您可以尝试preg_grep()以仅返回数组中的特定模式。下面

$email_only_array = preg_grep("/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/", $your_array); 

preg_grep("your Pattern", $array); 

检查样品:

// Your array 
$array = array("john","wxyz","[email protected]", "wxyz", "[email protected]", "[email protected]"); 
    // validating the value as array  
$email_only_array = preg_grep("/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/", $array);  
    // Output 
print_r($email_only_array); 

输出

Array ([2] => [email protected] [4] => [email protected] [5] => [email protected]) 

参考这也,http://php.net/manual/en/function.preg-grep.php

+0

非常感谢您的先生。我尝试了上面的代码,我在给定模式下像$ users一样传递数组,但它给出了空数组先生。请给我答复先生。 – sreedhar

+0

谢谢你先生,在这个问题上给予回复。我在我的代码中试过,它工作正常,先生,我能够以array的形式检索唯一的邮件。谢谢你,先生。 – sreedhar