Learn how to a build a cloud-first strategyRegister Now


Php, compare array with string and remove identical contain

Posted on 2011-10-30
Medium Priority
Last Modified: 2012-05-12
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:
[0] -> word
[1] -> eua
[2] -> statue
[3] -> game

How I do this?

The best regards, JC
Question by:Pedro Chagas

Assisted Solution

amigura earned 500 total points
ID: 37054502

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

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

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


Open in new window

LVL 10

Accepted Solution

Derokorian earned 1500 total points
ID: 37054513
$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 ) {

$array = array_values($array);


Open in new window


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question