Click here to Skip to main content
15,924,829 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
first array

VB
Array
(
    [0] => Array
        (
            [file] => newhotfolder.gif
            [path] => images/newhotfolder.gif
            [type] => gif
            [size] => 1074
            [md5] => 123812asdkbqw98eqw80hasdas234234
        )

    [1] => Array
        (
            [file] => image.gif
            [path] => images/attachtypes/image.gif
            [type] => gif
            [size] => 625
            [md5] => 7bbb66e191688a86b6f42a03bd412a6b
        )

    [2] => Array
        (
            [file] => header.gif
            [path] => images/attachtypes/header.gif
            [type] => gif
            [size] => 625
            [md5] => 71291239asskf9320234kasjd8239393
        )
)




second array

VB
Array
(
    [0] => Array
        (
            [file] => newhotfolder.gif
            [path] => images/newhotfolder.gif
            [type] => gif
            [size] => 1074
            [md5] => 8375h5910423aadbef67189c6b687ff51c
        )

    [1] => Array
        (
            [file] => image.gif
            [path] => images/attachtypes/image.gif
            [type] => gif
            [size] => 625
            [md5] => 7bbb66e191688a86b6f42a03bd412a6b
        )

    [2] => Array
        (
            [file] => footer.gif
            [path] => images/attachtypes/footer.gif
            [type] => gif
            [size] => 625
            [md5] => 1223819asndnasdn2213123nasd921
        )
)
Posted
Comments
Sergey Alexandrovich Kryukov 1-Jun-14 13:37pm    
Would you define what does it mean "find differences"? It will be not so trivial as you may think...
—SA
Member 10014068 29-Nov-16 18:22pm    
this one is quite good, but ...
it have big problem with comparing values retrieved by post submission against array with values retrieved from database. The biggest issue are 0 (zeros) submitted by form. PHP treats them as empty while those retrieved from database have value 0. any trick for it?

1 solution

These are multi-dimensional associative arrays, you can recursively do an array_diff_assoc, find out here: array-diff-assoc-php[^]
See example:
<?php
/*
[NOTE BY danbrown AT php DOT net: The array_diff_assoc_recursive function is a 
combination of efforts from previous notes deleted.
Contributors included (Michael Johnson), (jochem AT iamjochem DAWT com), 
(sc1n AT yahoo DOT com), and (anders DOT carlsson AT mds DOT mdh DOT se).]
*/
function array_diff_assoc_recursive($array1, $array2)
{
	foreach($array1 as $key => $value)
	{
		if(is_array($value))
		{
			if(!isset($array2[$key]))
			{
				$difference[$key] = $value;
			}
			elseif(!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;
}

$a1=Array
(
    "0" => Array
        (
            "file" => "newhotfolder.gif",
            "path" => "images/newhotfolder.gif",
            "type" => "gif",
            "size" => "1074",
            "md5" => "123812asdkbqw98eqw80hasdas234234"
        ),
 
    "1" => Array
        (
            "file" => "image.gif",
            "path" => "images/attachtypes/image.gif",
            "type" => "gif",
            "size" => "625",
            "[md5]" => "7bbb66e191688a86b6f42a03bd412a6b"
        ),
 
    "2" => Array
        (
            "file" => "header.gif",
            "path" => "images/attachtypes/header.gif",
            "type" => "gif",
            "size" => "625",
            "md5" => "71291239asskf9320234kasjd8239393"
        )
);
$a2=Array
(
    "0" => Array
        (
            "file" => "newhotfolder.gif",
            "path" => "images/newhotfolder.gif",
            "type" => "gif",
            "size" => "1074",
            "md5" => "8375h5910423aadbef67189c6b687ff51c"
        ),
 
    "1" => Array
        (
            "file" => "image.gif",
            "path" => "images/attachtypes/image.gif",
            "type" => "gif",
            "size" => "625",
            "[md5]" => "7bbb66e191688a86b6f42a03bd412a6b"
        ),
 
    "2" => Array
        (
            "file" => "header.gif",
            "path" => "images/attachtypes/footer.gif",
            "type" => "gif",
            "size" => "625",
            "md5" => "1223819asndnasdn2213123nasd921"
        )
);

print_r(array_diff_assoc_recursive($a1, $a2));

?>
 
Share this answer
 
v2
Comments
Joe Kelley 10-Nov-15 10:11am    
This solution worked perfectly for me. It would have been nice if you showed what print_r printed...but I tried it and it did exactly what I wanted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900