使用array_unique的php数组唯一值

时间:2018-07-31 07:08:47

标签: php arrays

Array
(
[0] => Array
    (
        [0] => abc@test.com
        [1] => qwrt@sometest.com
        [2] => haritha@elitesin.com
    )

[1] => Array
    (
        [0] => Kanishka.Kumarasiri@elitesin.com
        [1] => Haritha@elitesin.com
    )

[2] => Array
    (
        [0] => Kanishka.Kumarasiri@elitesin.com
        [1] => test@elitesin.com
    )

)

我有一个像这样的数组,我想从这个数组中获得唯一的值。

但是我的代码失败

for ($i = 0; $i < count($return_arr); $i++) {

$new_value[] = explode(",", $return_arr[$i]);
}

print_r (array_unique($new_value));

表示数组到字符串的转换错误

我希望数组像这样,仅获取唯一的电子邮件ID

 Array
(
[0] => Array
    (
        [0] => abc@test.com
        [1] => qwrt@sometest.com
        [2] => haritha@elitesin.com
        [3] => Kanishka.Kumarasiri@elitesin.com
        [4] => test@elitesin.com
    )

  )

1 个答案:

答案 0 :(得分:2)

由于您的代码错误,因此您试图爆炸数组中未包含的内容,请尝试以下代码:

    <?php
$arr = array("0"=>array("abc@test.com","qwrt@sometest.com","haritha@elitesin.com"),
          "1"=>array("Kanishka.Kumarasiri@elitesin.com,Haritha@elitesin.com"),
         "2"=>array("Kanishka.Kumarasiri@elitesin.com,test@elitesin.com"));
$allEmails = array();
foreach($arr as $array){
    foreach($array as $email){
          $allEmails[] = explode(",",$email);
        }
}
$new_value = array();    
foreach($allEmails as $array){
        foreach($array as $email){
          $new_value[] = strtolower($email);
        }
    }

    print_r (array_unique($new_value));

?>