PHP- I need to compare two arrays and find the values that match between them

Posted on 2006-04-04
Last Modified: 2008-02-01
Say i have 2 arrays
array 1 = 0-45, 1-14, 2-16, 3-25, 4-99

array 2 = 0-16 1-15 2-67 3-45 4-88

Is there a simple PHP function to return which values match in another array? In this case a comparative array that returns


since both arrays have those values?


Question by:stormist
    LVL 3

    Expert Comment

    array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

    Example 227. array_intersect() example

    $array1 = array("a" => "green", "red", "blue");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_intersect($array1, $array2);

    This makes $result have

    <?php Array
       [a] => green
       [0] => red
    ) ?>
    LVL 43

    Accepted Solution

    LVL 10

    Author Comment

    Hi all! What would be a way to use that function but reset the keys? first one 0 , 2nd one 1. etc?

    LVL 43

    Expert Comment

    on the resulted array. Resulting array would be indexed by numbers.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now