mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-07 12:36:25 +00:00
Improving performance of Set::diff()
This commit is contained in:
parent
0abe9afc15
commit
fde10b1e62
1 changed files with 9 additions and 18 deletions
|
@ -777,7 +777,8 @@ class Set {
|
||||||
*
|
*
|
||||||
* @param mixed $val1 First value
|
* @param mixed $val1 First value
|
||||||
* @param mixed $val2 Second value
|
* @param mixed $val2 Second value
|
||||||
* @return array Computed difference
|
* @return array Returns the key => value pairs that are not common in $val1 and $val2
|
||||||
|
* The expression for this function is ($val1 - $val2) + ($val2 - ($val1 - $val2))
|
||||||
* @access public
|
* @access public
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
|
@ -788,25 +789,15 @@ class Set {
|
||||||
if (empty($val2)) {
|
if (empty($val2)) {
|
||||||
return (array)$val1;
|
return (array)$val1;
|
||||||
}
|
}
|
||||||
$out = array();
|
$intersection = array_intersect_key($val1,$val2);
|
||||||
|
while (list($key,) = each($intersection)) {
|
||||||
foreach ($val1 as $key => $val) {
|
if ($val1[$key] == $val2[$key]) {
|
||||||
$exists = array_key_exists($key, $val2);
|
unset($val1[$key]);
|
||||||
|
unset($val2[$key]);
|
||||||
if ($exists && $val2[$key] != $val) {
|
|
||||||
$out[$key] = $val;
|
|
||||||
} elseif (!$exists) {
|
|
||||||
$out[$key] = $val;
|
|
||||||
}
|
|
||||||
unset($val2[$key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($val2 as $key => $val) {
|
|
||||||
if (!array_key_exists($key, $out)) {
|
|
||||||
$out[$key] = $val;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $out;
|
|
||||||
|
return $val1 + $val2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue