PHP array_diff() Function with Example

PHP array_diff() Function: Here, we will find out about array_diff() work with example in PHP.

PHP array_diff() Function

array_diff() work is a cluster work in PHP, it is utilized to discover the distinctions of at least two exhibits.

It looks at the values of given clusters and returns the values which are not regular in the exhibits.

It contrasts values of the main cluster and the subsequent exhibit and returns the values which are absent in the subsequent cluster.

Syntax:

array_diff(array1, array2, …) : array

Here, array1, array2 are the info exhibits, two cluster parameters are required. We can likewise give more clusters to analyze.

Example:

    Input:
	$arr1 = array("101" => "Kishan", "102" => "Durgesh", "103" => "Aakash");
	$arr2 = array("101" => "Kishan", "102" => "Durgesh");

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

    Output:
    Array
    (
        [103] => Aakash
    )

PHP Code:

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

Output:

Array
(
    [103] => Aakash
)

PHP Code:

<?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
)

Leave a Comment

error: Alert: Content is protected!!