Remove value from array if value is a substring of another value

Array
(
    [0] => tt0087523
    [1] => cehennem melekleri
    [3] => euer weg führt durch die hölle
    [5] => guerreiros selvagens
    [7] => jungel krigerne
    [9] => jungle fever
    [11] => jungle warriors
    [17] => jungle warriors euer weg führt durch die hölle
    [19] => la guerra de la coca
    [21] => les guerriers de la jungle
    [23] => los guerreros de la jungla
    [25] => the czar of brazil
    [27] => viidakkosoturit
)

How to remove all values ​​that are a substring of another value. For example, delete the index [3]because it is a substring [17], and also [11]because it is also a substring 17.

I am creating a string for text search and would like it to be as short as possible.

Update: re: comments :)

foreach ($array as $i => $value) {
    foreach ($array as $j => $search) {
        if ($i === $j) continue;
        if (false !== stripos($search, $value)) {
            unset($array[$i]);
        }
    }
}
+4
source share
3 answers

You can use two loops to check if the string is substr:

$array = array(
"0" => "tt0087523",
"1" => "cehennem melekleri",
"3" => "euer weg führt durch die hölle",
"5" => "guerreiros selvagens",
"7" => "jungel krigerne",
"9" => "jungle fever",
"11" => "jungle warriors",
"17" => "jungle warriors euer weg führt durch die hölle",
"19" => "la guerra de la coca",
"21" => "les guerriers de la jungle",
"23" => "los guerreros de la jungla",
"25" => "the czar of brazil",
"27" => "viidakkosoturit");

foreach($array as $key => $value){
    foreach($array as $key2 => $value_to_compare){
        if($key2 == $key)
            continue;

        $pos = strpos($value_to_compare, $value);

        if($pos !== false){
            unset($array[$key]);
        }
    }
}

print_r($array);
/*

Array
(
    [0] => tt0087523
    [1] => cehennem melekleri
    [5] => guerreiros selvagens
    [7] => jungel krigerne
    [9] => jungle fever
    [17] => jungle warriors euer weg führt durch die hölle
    [19] => la guerra de la coca
    [21] => les guerriers de la jungle
    [23] => los guerreros de la jungla
    [25] => the czar of brazil
    [27] => viidakkosoturit
)

*/
+2
source

What about...

array_walk($array, function(&$v,$k,&$a) {
  if(count(preg_grep('~'.preg_quote($v,'~').'~i',$a))>1)unset($a[$k]);
},&$array);

edit:

I tested this in php 5.3.22, and passing the function reference was removed 5.4, as suggested by OP, here is 5.4 + alt:

array_walk($array, function (&$v, $k) use (&$array) { 
  if(count(preg_grep('~'.preg_quote($v,'~').'~i', $array))>1)unset($array[$k]); 
});
+4

Crayon array_walk - , unset(), array_walk , .

Instead of unset (), replace this value with an empty string and run array_filter to remove the empty values ​​after the walk.

array_walk($array, function (&$v, $k) use (&$array) { 
  if(count(preg_grep('~'.preg_quote($v,'~').'~i', $array))>1)unset($array[$k]); 
    $array[$i] = '';
});
$array = array_filter($array);
0
source

All Articles