1

How can I delete duplicates from multiple arrays?

pinky and cocos are double in my array. All words which are double, must be removed. If those are removed, I will put these words in my select. I get those words from my database.

The query:

$queryClient = " SELECT DISTINCT `clients` FROM `reps` WHERE `clients` != ''";

This is my code:

while($row =  mysql_fetch_assoc($resultClient)){
    $names = explode(",", $row['clients']);
    echo '<pre>'; print_r($names); echo '</pre>';
}

Result: (Those food words are just an example)

Array
    (
        [0] => chocolate
    )
    Array
    (
        [0] => vanilla
        [0] => cocos
    )
    Array
    (
        [0] => strawberry
    )
    Array
    (
        [0] => pinky
        [1] => watermelon
        [2] => melon
        [3] => cocos
    )
    Array
    (
        [0] => pinky 
    )
    Array
    (
        [0] => dark-chocolate
    )

I tried this in my while loop but it did not work:

$array = array_unique($names, SORT_REGULAR);

How can I remove all duplicates? Can you help me or do you have a solution for my problem? Help.

3 Answers3

4

Here's a one-liner:

print_r(array_unique(call_user_func_array('array_merge', $names)));

First merge all subarrays into one, then get unique values.

Full example:

$names = array();
while($row =  mysql_fetch_assoc($resultClient)){
    $names[] = explode(",", $row['clients']);
}
print_r(array_unique(call_user_func_array('array_merge', $names)));
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Sir, this does not work! Please help me! while($row = mysql_fetch_assoc($resultClient)){ $names = explode(",", $row['clients']); echo '
    ' . print_r(array_unique(call_user_func_array('array_merge', $names))).'
    '; } @u_mulder
    –  Oct 24 '17 at 12:45
  • I understand you and this worked! but ... all of them needed to be **in** my select tag. I tried this but did not work: '.$names.''; } –  Oct 24 '17 at 14:00
  • Do you __understand__ what you do in your code? Why do you assign result of `print_r` to `$cucul`? Do you know what `print_r` returns? – u_mulder Oct 24 '17 at 14:36
1

You can just do a little trick:

Flatten, count and then remove all except the last.

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); 
$flatArray = [];
foreach($it as $v) {
   $flatArray[] = $v;           //Flatten array
}

//Note you can do array_unique on the flat array if you also need to flatten the array

$counts = array_count_values($flatArray); //Count 

foreach ($array as &$subarray) {
     foreach ($subarray as $index => $element) {
          $counts[$element]--;
          if ($counts[$element] > 0) { //If there's more than 1 left remove it
               unset($subarray[$index]);
          }
     }
} 

This will remove duplicates nested exactly on the 2nd level without flattening the original array.

http://sandbox.onlinephpfunctions.com/code/346fd868bc89f484dac48d12575d678f3cb53626

apokryfos
  • 38,771
  • 9
  • 70
  • 114
1

first you need to join your array before you can filter out the duplicates:

<?php
$allNames = [];
while($row =  mysql_fetch_assoc($resultClient)){
    $names = explode(",", $row['food']);
    $allNames[] = $names;
}

$allNames = array_merge(...$allNames);  //Join everything to a one dimensional array
$allNames = array_unique($allNames); // Only keep unique elementes

print_r($allNames);
Richard87
  • 1,592
  • 3
  • 16
  • 29
  • I tried your snippet, I get only one food word? strange isnt it? –  Oct 24 '17 at 12:21
  • 1
    What about this snippet? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Oct 24 '17 at 12:30