如果我給你的權利,我建議是這樣的:
$old_ids = array();
foreach ($old as $row) {
$old_ids[$row['id']] = $row;
}
$new_ids = array();
foreach ($new as $row) {
$new_ids[$row['id']] = $row;
}
$insert = array_diff_key($new_ids, $old_ids);
//^^^Returns all records of $new_ids that aren't present in $old_ids
$update = array_intersect_key($new_ids, $old_ids);
//^^^Returns all records of $new_ids that were present in $old_ids
$delete = array_diff_key($old_ids, $new_keys);
//^^^Returns all records of $old_ids that aren't present in $new_ids
你現在有3個陣列,所有的數據,我想的名字清楚地告訴他們該怎麼做。請記住,這些函數返回其第一參數數據,所以
array_intersect_key($new_ids, $old_ids) != array_intersect_key($old_ids, $new_ids)
爲array_intersect_key($new_ids, $old_ids)
將返回新數據,並array_intersect_key($old_ids, $new_ids)
將返回老的數據,你必須再次尋找新的價值。不要亂參錯誤順序。
請分享一些代碼。 「代碼片段勝過千言萬語」 – DhruvPathak 2011-03-17 10:40:50