-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?

3
  • 5
    So you want to use array_intersect(). Commented Nov 22, 2016 at 9: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 Commented Nov 22, 2016 at 9:52
  • 2
    duplicate of stackoverflow.com/questions/17648962/… Commented Nov 22, 2016 at 9:53

4 Answers 4

2

Use array_intersect()

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

Demo: https://eval.in/682653

Sign up to request clarification or add additional context in comments.

Comments

2

You can use array_intersect

$c = array_intersect($a, $b);

Comments

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

Comments

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)

?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.