Home » PHP

PHP array_diff() Function with Example

PHP array_diff() Function: Here, we are going to learn about array_diff() function with example in PHP.
Submitted by IncludeHelp, on February 08, 2019

PHP array_diff() Function

array_diff() function is an array function in PHP, it is used to find the differences of two or more arrays.

It compares the values of given arrays and returns the values which are not common in the arrays.

It compares values of the first array with the second array and returns the values which are not present in the second array.

Syntax:

    array_diff(array1, array2, ...) : array

Here, array1, array2 are the input arrays, two array parameters are required. We can also provide more arrays to compare.

Examples:

    Input:
	$arr1 = array("101" => "Amit", "102" => "Abhishek", "103" => "Prem");
	$arr2 = array("101" => "Amit", "102" => "Abhishek");

    Function call:
    array_diff($arr1, $arr2);

    Output:
    Array
    (
        [103] => Prem
    )

PHP code:

<?php
	$arr1 = array("101" => "Amit", "102" => "Abhishek", "103" => "Prem");
	$arr2 = array("101" => "Amit", "102" => "Abhishek");
	
	//finding & printing arrays
	$ans = array_diff($arr1, $arr2);
	print_r ($ans);
?>

Output

Array
(
    [103] => Prem
)
<?php
    $arr1 = array(10, 20, 30, 40, 50);
    $arr2 = array(10, 20, 60, 70, 70);
    print_r (array_diff($arr1, $arr2));
?>

Output

(
    [2] => 30
    [3] => 40
    [4] => 50
)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.