1

I have two arrays, I need to find out the value for each of the array which is the same.

For example,

   $arr1=array("a", "b", "c");
   $arr2=array("c", "d", "e");

Then c should be display. How could I do this?

3 Answers 3

2

You can use the array_intersect function to find common elements.

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

Comments

1
$word1 =array('a', 'b','c', 'd');
$word2 =array('b', 'c', 'd', 'a');
$data = array_intersect($word1, $word2);

it will return a,b,d because that is common in both array

print_r( $data );
/* result:
    Array (
             [0] => a
             [1] => b
              [3] => d 
    ) */

1 Comment

Rather than only post a block of code, please explain why this code solves the problem posed. Without an explanation, this is not an answer.
0

If you want to do it "manually", here is one way:

$a1 = array("a", "b", "c");
$a2 = array("c", "d", "e");

$a3 = array();
foreach($a1 as $x) foreach($a2 as $y) if($x == $y) $a3[] = $x;

print_r($a3);
// prints:
// Array
// (
//    [0] => c
// )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.