Reputation: 868
I got 2 tables like these:
$array1 = (2, 7, 9, 15);
$array2 = (3, 7, 10, 15);
I would like to get some kind of a mix of the 2 tables. Result has to be like:
$result = (2, 7, 9, 15, 3, 10);
Without any duplicate values. Thanks for your help.
Upvotes: 0
Views: 124
Reputation: 2113
You can do it programmatically like this:
<pre>
<?php
$array1 = array(2,7,9,15);
$array2 = array(3,7,10,15);
function array_merge_func($array1,$array2)
{
foreach($array1 as $v)
{
if(!in_array($v,$array2))
{
array_push($array2,$v);
}
}
return $array2;
}
$array3 = array_merge_func($array1,$array2);
print_r($array3);
?>
</pre>
Check if values from array1 doesnt exists in array2. If not exist push the value inside the array2.
Upvotes: 0
Reputation: 9823
//First merge the two arrays
$c = array_merge($array1,$array2);
//Then use the following function to return unique values only
$unique = array_unique($c);
Upvotes: 1
Reputation: 1557
Do something like:
$array1 = array(2, 7, 9, 15);
$array2 = array(3, 7, 10, 15);
$array3 = array_merge($array1, $array2);
$array3 = array_unique($array3);
Upvotes: 0
Reputation: 1881
You can loop through the second array and check if each value exists in the first:
foreach($array2 as $v) {
if(!in_array($v, $array1)) {
$array1[] = $v;
}
}
Upvotes: 0
Reputation: 18584
Why don't you try this:
$result = array_unique(array_merge($arra1, $array2));
see also the docs:
http://php.net/manual/en/function.array-merge.php
http://php.net/manual/en/function.array-unique.php
Upvotes: 2