Last active
September 12, 2019 12:31
-
-
Save PatricNox/85228cc3cda16372f280e0215b3305c0 to your computer and use it in GitHub Desktop.
Find identical content between two arrays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Find identical content between two arrays. | |
* | |
* | |
* @param Array $first_array | |
* @param Array $second_array | |
* @return Array | |
**/ | |
function _match_array_content(Array $first_array, Array $second_array): Array | |
{ | |
$matches = array(); | |
foreach ($first_array as $value) { | |
if (in_array($value, $second_array)) { | |
array_push($matches, $value); | |
} | |
} | |
return $matches; | |
} | |
// Test | |
$first="1,2,3,4,5,6,7,8,8,6"; | |
$second="4,2,5,6,7,8,9"; | |
$first=explode(',',$first); | |
$second=explode(',',$second); | |
var_dump(_compare_array_content($first, $second)); | |
// Simpler method | |
var_dump(array_intersect($first, $second)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment