PHP array_diff() function

This function is used to get array differences. This function returns those values from the first array that are not present in other arrays. So by using this, we can compare one array elements from one or more arrays elements.

Syntax

array_diff($array1, $array2, $array3, ...);
In this, we can pass two or more arrays in parameters. But it always returns array values from the first parameter that are not present in any of the other arrays.

Example1

<?php
    $arr1 = array('pen', 'pencil', 'book', 'copy');
    $arr2 = array('book', 'copy');
    $get_diff = array_diff($arr1, $arr2);
    print_r($get_diff);
?>		

Output :

Array ( [0] => pen [1] => pencil )

Example2

<?php
    $arr1 = array(1, 5, 7, 3 , 10);
    $arr2 = array(4, 5, 2, 5, 7, 89);
    $arr3 = array(5, 7, 4, 6, 8);
    $get_diff = array_diff($arr1, $arr2, $arr3);
    Print_r($get_diff);
?>		

Output :

Array ( [0] => 1 [3] => 3 [4] => 10 ) 




Related PHP Functions

PHP array_reverse() function
PHP array_key_exists() function
PHP array_push() function
PHP array_search() function
PHP class_exists() function
PHP curl_setopt() function
PHP die() function
PHP dirname() function
PHP each() function
PHP explode() function
PHP file_exists() function
PHP function_exists() function
PHP getenv() function
PHP is_readable() function
PHP ksort() function
PHP mkdir() function
PHP ob_start() function
PHP parse_url() function
PHP str_repeat() function
PHP substr() function




Read more articles


General Knowledge



Learn Popular Language