比较两个数组和数组的 return 差异

comparing two arrays and return difference of arrays

我的旧数组数据:

[Job] => Array
        (
            [id] => 2
            [job_state_id] => 14
            [assigned_to_id] => 
            [patient_id] => 2
            [prescription] => main
            [case_type_id] => 1
            [upper_midline_id] => 1
            [upper_midline_value] => 0
            [lower_midline_id] => 1
            [lower_midline_value] => 0
            [treat_arches] => 2
            [upper_midline_type_id] => 2
            [lower_midline_type_id] => 2
            [overjet] => 2
            [overbite] => 2
            [arch_form] => 2
            [canine_relationship] => 2
            [molar_relationship] => 1
            [posterior_crossbite] => 1
            [procline] => 2
            [expand] => 2
            [distalize] => 0
            [ipr] => 0
            [close_all_spaces] => 2
            [other_instructions] => other

和我的新数组数据:

[Job] => Array
        (
            [id] => 2
            [job_state_id] => 14
            [assigned_to_id] => 
            [patient_id] => 2
            [prescription] => main complain
            [case_type_id] => 1
            [upper_midline_id] => 1
            [upper_midline_value] => 0
            [lower_midline_id] => 1
            [lower_midline_value] => 0
            [treat_arches] => 2
            [upper_midline_type_id] => 2
            [lower_midline_type_id] => 2
            [overjet] => 2
            [overbite] => 2
            [arch_form] => 2
            [canine_relationship] => 2
            [molar_relationship] => 1
            [posterior_crossbite] => 1
            [procline] => 2
            [expand] => 2
            [distalize] => 0
            [ipr] => 1
            [close_all_spaces] => 2
            [other_instructions] => other instrucations

您可以看到一些值正在发生变化。我需要将 $new 数组数据与 $old 数据数组进行比较,并仅捕获对值所做的更改。 我用过这个代码:

$difference = array_diff($oldJobData, $newJobData);

差异变量return $oldJobData 我只想要差异而不是整个数组 我也使用了以下代码,但无法获得想要的结果。

          $new2 = array();
            foreach ($newJobData as $key => $new_val) {
                if (isset($oldJobData[$key])) { // belongs to old array?
                    if ($oldJobData[$key] != $new_val) // has changed?
                        $new2[$key] = $newJobData[$key]; // catch it
                }
            }

您有一个关联数组,因此必须使用 array_diff_assoc:

$changes = array_diff_assoc($new, $old);