[英]Set new /remove/changed key=>value pairs between two arrays
[英]Verifying key/value pairs between arrays
我有一個“主”陣列,我必須對照主陣列驗證幾個陣列。
主數組是鍵/值對的列表。 其他數組必須由這些對中的一些(或全部)組成, 別無其他 。
這是一些例子澄清:
$master = [1=>'foo', 2=>'bar', 3=>'baz'];
$good_child = [2=>'bar'];
$wrong_child_1 = [2=>'sparta'];
$wrong_child_2 = [42=>'bar'];
目前,我正在使用以下快速代碼進行驗證:
foreach ($child as $key => $value) {
if ($master[$key] !== $value) {
// wrong child
}
}
您可能已經注意到,上面的$wrong_child_2
(未定義的索引)會失敗,盡管在實際的應用程序中這不是問題(至少現在是這樣)。
我的問題是,會有更好的方法進行這些驗證嗎? 最好沒有循環,而是數組函數。
看看array_diff_assoc
。
$bad = (bool)count(array_diff_assoc($test_array, $master_array))
換句話說, $test_array
不驗證是否存在任何差異,包括索引檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.