【发布时间】:2013-11-15 21:01:41
【问题描述】:
我正在编写一个脚本 PHP 脚本,它每 24 小时创建一个所选目录中所有文件的报告。我想将最新报告与以前的报告进行比较,以检测文件的任何编辑(通过比较 md5 哈希)、文件的删除和文件的创建。我将两个报告的数据放在两个单独的数组中。据我所知,我将不得不使用 array_diff 函数。我该怎么做:A) 将它与多维数组一起使用,B) 如果差异是编辑、删除或创建,则使用标签。
数据示例:
新报告:
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
)
)
旧报告:
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
)
)
该函数必须能够检测到“newhotfolder.gif”的 md5 哈希值已更改,文件“footer.gif”已删除以及“header.gif”已添加。也许像这样返回第三个数组?:
比较:
Array
(
[0] => Array
(
[file] => newhotfolder.gif
[path] => images/newhotfolder.gif
[type] => gif
[size] => 1074
[md5] => 8375h5910423aadbef67189c6b687ff51c
[status] => edited
)
[1] => Array
(
[file] => image.gif
[path] => images/attachtypes/image.gif
[type] => gif
[size] => 625
[md5] => 7bbb66e191688a86b6f42a03bd412a6b
[status] => same
)
[2] => Array
(
[file] => footer.gif
[path] => images/attachtypes/footer.gif
[type] => gif
[size] => 625
[md5] => 1223819asndnasdn2213123nasd921
[status] => deleted
)
[3] => Array
(
[file] => header.gif
[path] => images/attachtypes/header.gif
[type] => gif
[size] => 625
[md5] => 71291239asskf9320234kasjd8239393
[status] => new
)
)
【问题讨论】:
-
根据该链接,“此函数仅检查 n 维数组的一维。当然,您可以使用 array_diff($array1[0], $array2[0]); 检查更深的维度。 . "
-
@EmmyS 做不到。
标签: php arrays array-difference