Skip to content

Instantly share code, notes, and snippets.

@PatricNox
Last active September 12, 2019 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatricNox/85228cc3cda16372f280e0215b3305c0 to your computer and use it in GitHub Desktop.
Save PatricNox/85228cc3cda16372f280e0215b3305c0 to your computer and use it in GitHub Desktop.
Find identical content between two arrays.
<?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