Php, compare array with string and remove identical contain

Posted on 2011-10-30
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
    LVL 7

    Assisted Solution


    $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

    $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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to dynamically set the form action using jQuery.
    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 …

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now