array_intersect and array_intersect_assoc

array_intersect and array_intersect_assoc

- These both functions are basically used to find the common elements between two arrays.
- Basic difference above function is
array_intersect()
- returns the common elements which are having same value
array_intersect_assuc()
- returns the common elements which are having same key and value.

Example:
$arr1 = array("index1" => "one", "index2" => "two", "index3" => "three", "four");
$arr2 = array("index1" => "one", "three","four");

$resArr = array_intersect_assoc($arr1, $arr2);
print_r($resArr);

$resArr2 = array_intersect($arr1, $arr2);
print_r($resArr2);

In the above example first print_r() will give only one value(one). Because only one element in both arrays are having same key and value.

Second print_r() will give one,three and four.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.