Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP compare two arrays and get the matched values not the difference

I'm trying to compare two arrays and get only the values that exist on both arrays but, unfortunately, I can't find the right array function to use...

I found the array_diff() function: http://php.net/manual/en/function.array-diff.php

But it's for the difference of the both arrays.

Example:

$array1 = array("**alpha**","omega","**bravo**","**charlie**","**delta**","**foxfrot**"); $array2 = array("**alpha**","gamma","**bravo**","x-ray","**charlie**","**delta**","halo","eagle","**foxfrot**"); 

Expected Output:

$result = array("**alpha**","**bravo**","**charlie**","**delta**","**foxfrot**"); 
like image 648
Julian Paolo Dayag Avatar asked Apr 02 '12 00:04

Julian Paolo Dayag


People also ask

How can I find matching values in two arrays PHP?

The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

How do you compare two arrays if they are equal or not?

Check if two arrays are equal or not using Sorting Follow the steps below to solve the problem using this approach: Sort both the arrays. Then linearly compare elements of both the arrays. If all are equal then return true, else return false.

How can I compare two arrays are equal in PHP?

The proper way to compare whether two arrays are equal is to use strict equality (===), which compares recursively.

Which PHP functions compare arrays and return the differences?

The array_diff() function compares the values of two (or more) arrays, and returns the differences.


2 Answers

Simple, use array_intersect() instead:

$result = array_intersect($array1, $array2); 
like image 112
Alix Axel Avatar answered Oct 07 '22 16:10

Alix Axel


OK.. We needed to compare a dynamic number of product names...

There's probably a better way... but this works for me...

... because....Strings are just Arrays of characters.... :>}

//  Compare Strings ...  Return Matching Text and Differences with Product IDs...  //  From MySql... $productID1 = 'abc123'; $productName1 = "EcoPlus Premio Jet 600";     $productID2 = 'xyz789'; $productName2 = "EcoPlus Premio Jet 800";     $ProductNames = array(     $productID1 => $productName1,     $productID2 => $productName2 );   function compareNames($ProductNames){         //  Convert NameStrings to Arrays...         foreach($ProductNames as $id => $product_name){         $Package1[$id] = explode(" ",$product_name);         }      // Get Matching Text...     $Matching = call_user_func_array('array_intersect', $Package1 );     $MatchingText = implode(" ",$Matching);      //  Get Different Text...     foreach($Package1 as $id => $product_name_chunks){         $Package2 = array($product_name_chunks,$Matching);         $diff = call_user_func_array('array_diff', $Package2 );         $DifferentText[$id] = trim(implode(" ", $diff));     }      $results[$MatchingText]  = $DifferentText;                   return $results;     }  $Results =  compareNames($ProductNames);  print_r($Results);  // Gives us this... [EcoPlus Premio Jet]          [abc123] => 600         [xyz789] => 800 
like image 21
Bill Warren Avatar answered Oct 07 '22 16:10

Bill Warren