note 33946 modified in function.array-diff-assoc by aidan

From: Date: Tue, 08 Jun 2004 14:18:42 +0000
Subject: note 33946 modified in function.array-diff-assoc by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-70458@lists.php.net to get a copy of this message
[anders dot carlsson at mds dot mdh dot se] The user contributed array_diff_assoc_recursive function is good except for the original array_diff_assoc always (?) returns an array. Therefore I propose that $difference is initially set to an empty array (and the array is always returned), and the comparison against FALSE is replaced by count($new_diff). At least that's the modifications I made to run it they way my code expects. --------------------------------------------- The following will recursively do an array_diff_assoc, which will calculate differences on a multi-dimensional level. (Forgive me if the braces do not line up, the note script did not like my tabs, and gave me trouble on some spaces.) <?php function array_diff_assoc_recursive($array1, $array2) { foreach($array1 as $key => $value) { if(is_array($value)) { if(!is_array($array2[$key])) { $difference[$key] = $value; } else { $new_diff = array_diff_assoc_recursive($value, $array2[$key]); if($new_diff != FALSE) { $difference[$key] = $new_diff; } } } elseif(!isset($array2[$key]) || $array2[$key] != $value) { $difference[$key] = $value; } } return !isset($difference) ? 0 : $difference; } ?> --was-- The following will recursively do an array_diff_assoc, which will calculate differences on a multi-dimensional level. (Forgive me if the braces do not line up, the note script did not like my tabs, and gave me trouble on some spaces.) <?php function array_diff_assoc_recursive($array1, $array2) { foreach($array1 as $key => $value) { if(is_array($value)) { if(!is_array($array2[$key])) { $difference[$key] = $value; } else { $new_diff = array_diff_assoc_recursive($value, $array2[$key]); if($new_diff != FALSE) { $difference[$key] = $new_diff; } } } elseif(!isset($array2[$key]) || $array2[$key] != $value) { $difference[$key] = $value; } } return !isset($difference) ? 0 : $difference; } ?> http://www.php.net/manual/en/function.array-diff-assoc.php

« previous php.notes (#70458) next »