Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Php, compare array with string and remove identical contain

Posted on 2011-10-30
2
Medium Priority
?
261 Views
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:
array
[0] -> word
[1] -> eua
[2] -> statue
[3] -> game

How I do this?

The best regards, JC
0
Comment
Question by:Pedro Chagas
2 Comments
 
LVL 7

Assisted Solution

by:amigura
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);

print_r($different);

Open in new window

0
 
LVL 10

Accepted Solution

by:
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 ) {
      unset($array[$key]);
   }
}

$array = array_values($array);

print_r($array);

Open in new window

0

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