Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- Откриване на разликите според двата масива, а не само според първия
- */
- $arr1 = array(
- 'key1' => 2,
- 'key2' => 4,
- 'key3' => array(1, 2,
- array(1,4,3,5,
- array(10, 13, 20)),
- 7),
- 'key4' => 1,
- );
- $arr2 = array(
- 'key1' => 2,
- 'key2' => 5,
- 'key3' => array(1, 2,
- array(1,34,3,5,
- array(10, 15, 20)),
- 7),
- );
- function array_diff_recursive($a1, $a2) {
- $result = array();
- $loopArray = (sizeof($a1) >= sizeof($a2)) ? $a1 : $a2;
- $len = sizeof($loopArray);
- $quit = false;
- for ( $i = 0; $i < $len; $i++ ) {
- $oppositeArray = ($loopArray === $a1) ? $a2 : $a1;
- $oppositeArrayKeys = array_keys($oppositeArray);
- $loopArrayKeys = array_keys($loopArray);
- if ( array_key_exists($loopArrayKeys[$i], $oppositeArray) ) {
- if ( is_array($loopArray[$loopArrayKeys[$i]]) && is_array($oppositeArray[$loopArrayKeys[$i]]) ) {
- $result[$loopArrayKeys[$i]] = array_diff_recursive($loopArray[$loopArrayKeys[$i]], $oppositeArray[$loopArrayKeys[$i]]);
- }
- else {
- if ( $loopArray[$loopArrayKeys[$i]] !== $oppositeArray[$loopArrayKeys[$i]] ) {
- $result[$loopArrayKeys[$i]] = $loopArray[$loopArrayKeys[$i]];
- }
- }
- } else {
- $result[$loopArrayKeys[$i]] = $loopArray[$loopArrayKeys[$i]];
- }
- if ( $i == ($len-1) && !$quit ) {
- $temp = $oppositeArray;
- $oppositeArray = $loopArray;
- $loopArray = $temp;
- $i = 0;
- $quit = true;
- $len = sizeof($loopArray);
- }
- }
- return $result;
- }
- $multi_diff = array_diff_recursive($arr1, $arr2);
- // Tests
- echo "<pre>";
- echo "Array 1:\n";
- print_r($arr1);
- echo "\n\nArray 2:\n";
- print_r($arr2);
- echo "\n\nMulti diff:\n";
- print_r($multi_diff);
- echo "</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement