tags:

views:

111

answers:

5

I want to get the same value from two arrays. Example:

a[] = array(a,b,c,d,e,f,g,h);
b[] = array(c,d,o,l,p,i,u,y);

I want c[]=c,d;

+9  A: 

see http://docs.php.net/array_intersect:

array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.
$a = array('a','b','c','d','e','f','g','h');
$b = array('c','d','o','l','p','i','u','y');
$c = array_intersect($a, $b);
var_dump($c);

prints

array(2) {
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
}
VolkerK
to get rid of the keys, you can use `array_values(array_intersect($a, $b))`
Marius
A: 

in_array() might be what you're looking for

Use in_array() to see if the value you're looking for is in both arrays a and b and then put it in array c (following your example)

http://www.php.net/manual/en/function.in-array.php

Ganesh Shankar
+3  A: 

Try $result = array_intersect($a, $b);

chelmertz
A: 

Use array_intersect($a,$b) -- Ohh many guys answered before i typed

Webbisshh
+3  A: 
<?php

$arr = array_intersect(array('a', 'b', 'c', 'd'),
                       array('c', 'd', 'e', 'f'));

print_r(array_values($arr));
Richard Knop