Q:

Write a PHP function to set union of two arrays

0

Write a PHP function to set union of two arrays.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function array_union($x, $y)
   { 
      $aunion=  array_merge(
            array_intersect($x, $y),
            array_diff($x, $y),     
            array_diff($y, $x)      
        );
        return $aunion;
   }
$a = array(1, 2, 3, 4);
$b = array(2, 3, 4, 5, 6);
print_r(array_union($a, $b));
?>
Sample Output:
Array                                                       
(                                                           
    [0] => 2                                                
    [1] => 3                                                
    [2] => 4                                                
    [3] => 1                                                
    [4] => 5                                                
    [5] => 6                                                
)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now