note 92579 added to function.array-diff-assoc
| From: | phpdotnotesatdwarvendotcodotuk at osu1 dot php dot net | Date: | Thu, 30 Jul 2009 08:15:28 +0000 |
| Subject: | note 92579 added to function.array-diff-assoc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158901@lists.php.net to get a copy of this message | ||
A quick update to array_compare from 55 dot php at imars dot com, deals with array keys existing but
containing null, which otherwise fools the isset() function.
I replaced with array_key_exists():
<?php
// dwarven Differences:
// * Replaced isset() with array_key_exists() to account for keys with null contents
// Previously by 55 dot php at imars dot com
// Key differences:
// * Removed redundant test;
// * Returns false bool on exact match (not zero integer);
// * Use type-precise comparison "!==" instead of loose "!=";
// * Detect when $array2 contains extraneous elements;
// * Returns "before" and "after" instead of only "before" arrays on
mismatch.
function array_compare($array1, $array2) {
$diff = false;
// Left-to-right
foreach ($array1 as $key => $value) {
if (!array_key_exists($key,$array2)) {
$diff[0][$key] = $value;
} elseif (is_array($value)) {
if (!is_array($array2[$key])) {
$diff[0][$key] = $value;
$diff[1][$key] = $array2[$key];
} else {
$new = array_compare($value, $array2[$key]);
if ($new !== false) {
if (isset($new[0])) $diff[0][$key] = $new[0];
if (isset($new[1])) $diff[1][$key] = $new[1];
};
};
} elseif ($array2[$key] !== $value) {
$diff[0][$key] = $value;
$diff[1][$key] = $array2[$key];
};
};
// Right-to-left
foreach ($array2 as $key => $value) {
if (!array_key_exists($key,$array1)) {
$diff[1][$key] = $value;
};
// No direct comparsion because matching keys were compared in the
// left-to-right loop earlier, recursively.
};
return $diff;
};
?>
----
Server IP: 87.124.35.190
Probable Submitter: 89.105.107.98
----
Manual Page -- http://www.php.net/manual/en/function.array-diff-assoc.php
Edit -- https://master.php.net/note/edit/92579
Del: integrated -- https://master.php.net/note/delete/92579/integrated
Del: useless -- https://master.php.net/note/delete/92579/useless
Del: bad code -- https://master.php.net/note/delete/92579/bad+code
Del: spam -- https://master.php.net/note/delete/92579/spam
Del: non-english -- https://master.php.net/note/delete/92579/non-english
Del: in docs -- https://master.php.net/note/delete/92579/in+docs
Del: other reasons-- https://master.php.net/note/delete/92579
Reject -- https://master.php.net/note/reject/92579
Search -- https://master.php.net/manage/user-notes.php