How to merge these two arrays:
$arr1 = array ( 1, 3, 2 );
$arr2 = array ( 1, 2, 4 );
So the result is an array with only the values found in both:
$result = array ( 1, 2 );
CodePudding user response:
Use the array_intersect() function that "returns an array containing all the values of array that are present in all the arguments. Note that keys are preserved."
<?php
$arr1 = [ 1, 3, 2 ];
$arr2 = [ 1, 2, 4 ];
$result = array_intersect($arr1, $arr2);
?>
Output:
Array ( [0] => 1 [2] => 2 )
You can use the array_values() to "return all the values from the array and index the array numerically."
$result = array_values(array_intersect($arr1, $arr2));
Output:
Array ( [0] => 1 [1] => 2 )