0

This is my array:

Array (
    [0] => Sos, Ser
    [1] => Sos, Ser, Pieczarki
    [2] => Sos, Ser, Szynka
    [3] => Sos, Ser, Salami
    [4] => Sos, Ser, Szynka, Pieczarki
    [5] => Sos, Ser, Szynka, Ananas
    [6] => Sos, Ser, Salami, Pieczarki
    [7] => Sos, Ser, Tunczyk, Cebula
)

I want to delete duplicates. array_unique() doesn't work. Desired result:

$result = array('Sos', 'Ser', 'Pieczarki', 'Szynka', 'Salami', 'Ananas','Tunczyk','Cebula');
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Emil Piter
  • 23
  • 3
  • 3
    Can you post the final result array you want? – Ananth Feb 06 '17 at 19:03
  • i want to only delete dplicates like Sos, Ser, Szynka, Salami from this array – Emil Piter Feb 06 '17 at 19:07
  • "Delete" doesn't give 100% clarity here. Do you want these values removed entirely? Do you want to preserve first occurrence? Do you want duplicated values stored in another var? – Ananth Feb 06 '17 at 19:08
  • Can you show us the expected output of that array? – Qirel Feb 06 '17 at 19:10
  • $result = array('Sos', 'Ser', 'Pieczarki', Szynka', 'Salami', 'Ananas','Tunczyk','Cebula'); – Emil Piter Feb 06 '17 at 19:10
  • Maybe an array is not the best data structure for this case. Consider using a hash set or similar. – Gabriel Feb 06 '17 at 19:18
  • I guess, you can use something like this: `$result = array_unique(call_user_func_array('array_merge', array_map(function($v){return array_map('trim', $v);}, array_map(function($v){return explode(',', $v);}, $source_array_of_strings))));` :D ... It could seems too complex, but it's not... you'll see ) – Wizard Feb 07 '17 at 00:33

4 Answers4

0

Based on the final array output you want, the code should be something like this:

<?php

$original = array("Sos, Ser",
    "Sos, Ser, Pieczarki",
    "Sos, Ser, Szynka",
    "Sos, Ser, Salami",
    "Sos, Ser, Szynka, Pieczarki",
    "Sos, Ser, Szynka, Ananas",
    "Sos, Ser, Salami, Pieczarki",
    "Sos, Ser, Tunczyk, Cebula",
);

$uniques = array();
// Loop over each original string
foreach ($original as $st) {
    // Split at ', ' to extract names from each string
    $names = explode(", ", $st);
    foreach ($names as $name) {
        // Store this name as a key in the uniques array
        $uniques[$name] = 1;
    }
}

$uniques = array_keys($uniques);
print_r($uniques);

/* Output:

Array
(
    [0] => Sos
    [1] => Ser
    [2] => Pieczarki
    [3] => Szynka
    [4] => Salami
    [5] => Ananas
    [6] => Tunczyk
    [7] => Cebula
)
*/
Ananth
  • 4,227
  • 2
  • 20
  • 26
0

Assuming your array is $array.

    $all_items = [];

    foreach ($array as $a1) {
        foreach ($a1 as $a2) {
            if (!in_array($a2, $all_items)) {
                $all_items[] = $a2;
            }
        }
    }

    return $all_items;
zed
  • 3,180
  • 3
  • 27
  • 38
0

Create an array of the exploded values and array_unique() that:

$result = array();

foreach($array as $value) {
    $result = array_merge($result, explode(', ', $value));
}
$result = array_unique($result);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

in_array always helps:

#!/usr/bin/env php
<?php

$aLong = [
    'Sos, Ser',
    'Sos, Ser, Pieczarki',
    'Sos, Ser, Szynka',
    'Sos, Ser, Salami',
    'Sos, Ser, Szynka, Pieczarki',
    'Sos, Ser, Szynka, Ananas',
    'Sos, Ser, Salami, Pieczarki',
    'Sos, Ser, Tunczyk, Cebula',
];

$aTotal = [];
foreach ($aLong as $sSub) {
    foreach (preg_split('/, */', $sSub) as $sWord) {
        if (!in_array($sWord, $aTotal)) {
            $aTotal[] = $sWord;
        }
    }
}

print_r($aTotal);
CharlieH
  • 1,432
  • 2
  • 12
  • 19