What PHP functions can be used to merge and compare arrays effectively?
When working with arrays in PHP, it is common to need to merge two arrays together or compare them to find differences. Two useful functions for merging arrays are array_merge() and array_merge_recursive(). For comparing arrays, you can use array_diff() to find the differences between two arrays, and array_intersect() to find the common elements between two arrays.
// Merge two arrays
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$mergedArray = array_merge($array1, $array2);
// Merge two arrays recursively
$array1 = ['a' => 1, 'b' => 2];
$array2 = ['a' => 3, 'c' => 4];
$mergedArray = array_merge_recursive($array1, $array2);
// Compare two arrays to find differences
$array1 = [1, 2, 3];
$array2 = [2, 3, 4];
$diffArray = array_diff($array1, $array2);
// Compare two arrays to find common elements
$array1 = [1, 2, 3];
$array2 = [2, 3, 4];
$intersectArray = array_intersect($array1, $array2);