1
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
    )

)

I have an array like this and I want to get unique values from this array.

But my code is failing

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

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

print_r (array_unique($new_value));

It says array to string conversion error

i want the array to be like this get only the unique email ids

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

  )
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
kanishka
  • 89
  • 2
  • 9
  • but it still return Kanishka.Kumarasiri@elitesin.com two times from Rays code – kanishka Jul 31 '18 at 08:05
  • seems like you are having multiple arrays within one array, thats why the error happen extract the master array first then work with the sub arrays inside it – The Doctor May 29 '23 at 15:23

2 Answers2

2

Because your code is wrong, you are trying to explode something which is not contained in your array, try this code:

<?php
$arr = [
    ["abc@test.com", "qwrt@sometest.com", "haritha@elitesin.com"],
    ["Kanishka.Kumarasiri@elitesin.com,Haritha@elitesin.com"],
    ["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));

?>
Levi
  • 661
  • 7
  • 26
Ray A
  • 1,283
  • 2
  • 10
  • 22
  • i want to get the unique email addresses from above array but code still returns duplicate values – kanishka Jul 31 '18 at 07:45
  • I updated the code, I added strtolower to make sure all emails are in lower letters so it can be compared, test and let me know if it works – Ray A Jul 31 '18 at 07:52
  • it still returns kanishka.kumarasiri@elitesin.com two times and haritha@elitesin.com two times – kanishka Jul 31 '18 at 07:55
  • I just tested the code again with the same emails you put in your question, non of them returned twice, – Ray A Jul 31 '18 at 08:04
  • Array ( [0] => abc@test.com,qwrt@sometest.com,haritha@elitesin.com [1] => kanishka.kumarasiri@elitesin.com,haritha@elitesin.com [2] => kanishka.kumarasiri@elitesin.com,test@elitesin.com ) that is what i code from the above code may i see you results as well – kanishka Jul 31 '18 at 08:09
  • I posted the complete code, test it and let me know – Ray A Jul 31 '18 at 08:14
  • there is a problem in your code, you are using comma to separate the emails, you didn't show that in your code you posted, – Ray A Jul 31 '18 at 08:15
  • I updated my code again, this time, it should be what you are looking for. – Ray A Jul 31 '18 at 08:22
  • yes now it works after your second update. yes i need to run for each tow times – kanishka Jul 31 '18 at 08:28
0

You don't need to add any more loops, just push one or more elements into the result array per iteration by using the spread operator on the return value from explode().

If your task requires case-insensitive uniqueness, you can most simply convert the unexploded string to lowercase.

$new_value = [];
foreach ($return_arr as $csv) {
    array_push($new_value, ...explode(",", strtolower($csv)));
}
var_export(array_unique($new_value));

This will be a flat array of unique values.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136