Php, compare array with string and remove identical contain

Hi E's, I need to compare one array with a variable, and remove from array the identical results, like:
In practice:
$array = array(word, rock, eua, statue, music, game);
$string = "rock music";

In the end the output is:
array
[0] -> word
[1] -> eua
[2] -> statue
[3] -> game

How I do this?

The best regards, JC
LVL 3
Pedro ChagasWebmasterAsked:
Who is Participating?
 
DerokorianConnect With a Mentor Commented:
$array = array('word', 'rock', 'eua', 'statue', 'music', 'game');
$string = "rock music";

$words = explode(' ',$string);

foreach( $words as $word ) {
   if( ($key = array_search($word,$array)) !== FALSE ) {
      unset($array[$key]);
   }
}

$array = array_values($array);

print_r($array);

Open in new window

0
 
amiguraConnect With a Mentor Commented:

$array1 = array(word, rock, eua, statue, music, game);
$string = "rock music";

$stringtoarray = explode(" ", $string);

$different = array_diff($array1, $stringtoarray);

print_r($different);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.