-2

I have two arrays as follows:-

$a = ["2","11","6"];
$b = ["6","7"];
$c = array_diff($a, $b);

$c = ["2","11"];

The result in $c is wrong. I want the result should be as $c = [6]

in other words i want the common elements in both array be returned! but it is giving wrong error. Kindly help me?

TIGER
  • 2,864
  • 5
  • 35
  • 45
Jamshaid Sabir
  • 73
  • 2
  • 10
  • 5
    So you want to use `array_intersect()`. – Rizier123 Nov 22 '16 at 09:51
  • 3
    Your title says you're looking for the difference, you're using the function to get the difference... but you want the ones that _aren't_ different? O_o – Jonnix Nov 22 '16 at 09:52
  • 2
    duplicate of http://stackoverflow.com/questions/17648962/how-to-get-common-values-from-two-different-arrays-in-php – TIGER Nov 22 '16 at 09:53

4 Answers4

2

Use array_intersect()

$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a, $b);

Demo: https://eval.in/682653

RJParikh
  • 4,096
  • 1
  • 19
  • 36
2

You can use array_intersect

$c = array_intersect($a, $b);
TIGER
  • 2,864
  • 5
  • 35
  • 45
2
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a,$b);
Muhammad Faizan Uddin
  • 1,339
  • 12
  • 29
0

Use array_intersect (http://php.net/manual/en/function.array-intersect.php)

<?php
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a, $b);

print_r($c)

?>
Rav
  • 1,327
  • 3
  • 18
  • 32