Last active
October 11, 2016 21:20
-
-
Save dlundgren/6551b53ac7f623f7129c98beb17f38f3 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Similar to array_diff() but works with the base multidimensional arrays | |
* | |
* @params array ... | |
* @return array | |
*/ | |
function arrayDiff() | |
{ | |
$arys = func_get_args(); | |
$prime = array_shift($arys); | |
$results = array(); | |
foreach ($prime as $key => $value) { | |
$found = false; | |
foreach ($arys as $ary) { | |
if (array_search($value, $ary) !== false) { | |
$found = true; | |
} | |
} | |
if (!$found) { | |
$results[$key] = $value; | |
} | |
} | |
return $results; | |
} | |
function altArrayDiff() | |
{ | |
$arys = func_get_args(); | |
$prime = array_shift($arys); | |
foreach ($prime as $key => $value) { | |
foreach ($arys as $ary) { | |
if (array_search($value, $ary) !== false) { | |
$prime[$key] = null; | |
break; | |
} | |
} | |
} | |
return array_filter($prime); | |
} | |
function anyArrayDiff() | |
{ | |
$results = array(); | |
foreach (func_get_args() as $ary) { | |
foreach ($ary as $key => $value) { | |
$rKey = array_search($value, $results); | |
if ($rKey === false) { | |
if (array_key_exists($key, $results)) { | |
$results[] = $value; | |
} | |
else { | |
$results[$key] = $value; | |
} | |
} | |
else { | |
unset($results[$rKey]); | |
} | |
} | |
} | |
return array_filter($results); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment