0

I Have two arrays like this

$array1 = Array ( [0] => Dutch [1] => Kannada [2] => Vietnamese [3] => Belarusian );
$array2 = Array ( [0] => English [1] => Germany [2] => Vietnamese [3] => Spanish [4] => Hindi );

I want to get the same values present in both arrays. So for that I used array_intersect like this

<?php
$array1 = Array ( [0] => Dutch [1] => Kannada [2] => Vietnamese [3] => Belarusian );
$array2 = Array ( [0] => English [1] => Germany [2] => Vietnamese [3] => Spanish )
$result = array_intersect($array1, $array2);
print_r($result);
?>

But its not showing the matching values. Even it is showing the error like

Parse error: syntax error, unexpected '[', expecting ')'

I think the array code structure is fine then why I am getting this error and not getting the matching values from two arrays? Any help and suggestions will be really appreciable. Thanks

2
  • The output of print_r() is not valid PHP array syntax. Commented Dec 11, 2014 at 9:53
  • Newuser This error is becaouse of your array element are not comma(,) seprated. So after each element its expecting array to be closed. It sould be , seprated like this $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); Commented Dec 11, 2014 at 10:08

2 Answers 2

3

This should work for you:

(First you declared your array wrong and forgot a ;)

$array1 = array ("Dutch", "Kannada", "Vietnamese", "Belarusian");
$array2 = array ("English", "Germany", "Vietnamese", "Spanish");
$result = array_intersect($array1, $array2);
print_r($result);

Output:

Array ( [2] => Vietnamese )
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

<?php 

$array1 = Array ( '0' => 'Dutch', '1' => 'Kannada', '2' => 'Vietnamese', '3' => 'Belarusian' );
$array2 = Array ( '0' => 'English', '1' => 'Germany','2' => 'Vietnamese', '3' => 'Spanish' );
$result = array_intersect($array1, $array2);
print_r($result);
?>

output:Array ( [2] => Vietnamese )

1 Comment

You should have quotes around the strings Dutch, Kannada, etc.

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.