4
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 would I remove all values that are a substring of another value. For example remove index [3] as it is a substring of [17] and also [11] as that is also a substring of 17.

I am building a string for text search and wanted 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
  • 5
    what exactly have you tried? Commented Nov 4, 2013 at 0:29
  • 3
    We prefer questions like: I want to do this and got stuck here rather than: I want to do this tell me how. Anyway, as far as I know you would have to have to loop through every element and compare it to every other element. Commented Nov 4, 2013 at 0:30
  • @CrayonViolent Point taken! Just off the back of a bunch of other problem solving and was lost temporarily :) Commented Nov 4, 2013 at 0:40
  • @user2856585 bah.. seems you accepted an answer already :/ I posted my take anyways Commented Nov 4, 2013 at 1:07

3 Answers 3

4

How 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 by reference to a function has been removed by 5.4 so as OP suggested, here is the 5.4+ alt:

array_walk($array, function (&$v, $k) use (&$array) { 
  if(count(preg_grep('~'.preg_quote($v,'~').'~i', $array))>1)unset($array[$k]); 
});
Sign up to request clarification or add additional context in comments.

8 Comments

As I accepted an answer that was the same as my own solution. This has to be the winner, two new functions for me to learn :) Thank you!
@user2856585 note that this is a case-insensitive comparison. If you want it to be case-sensitive, remove the i modifier in that preg_grep
Fatal error: Call-time pass-by-reference has been removed using PHP 5.5.5
oh snap.. i tested in 5.3.22
array_walk($array, function (&$v, $k) use (&$array) { if (count(preg_grep('~'.preg_quote($v).'~i', $array)) > 1) unset($array[$k]); });
|
2

You can use two loop to check if 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
)

*/

Comments

0

Crayon's array_walk is a neat solution except for the unset() which changes the array index and causes the array_walk to skip a record, hence some duplicates can be left in there.

Instead of unset(), replace that value with an empty string and run array_filter to remove 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.