How To Use Array Diff Function With Example Using PHP

admin_img Posted By Bajarangi soft , Posted On 21-09-2020

The array_diff function used to compare the array elements or values of two arrays, and return the differences,array_diff is in-built function in PHP

array_diff() function with example

Syntax array_diff() function in PHP

array_diff(array1,array2,array3,array4...);

 

Parameter   Description

array1       The array to compare from
array2       An array to compare against
array3       More arrays to compare against
array4,...   More arrays to compare against


Example(1)

$a1=array("a"=>"bike1","b"=>"bike2","c"=>"bike3","d"=>"scooter");
$a2=array("e"=>"bike1","f"=>"bike2","g"=>"bike3");

$result=array_diff($a1,$a2);
print_r($result);


In above example we have two array by using array_diff() function we can check which which array different from another


Complete code of array_diff()  function.
 

<!DOCTYPE html>
<html>
<head>
    <title>How to use array_diff() function with example in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>

<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>PHP array_diff() function </h1>
    </div>
    <br>
    <br>
    <h2></h2>
    <div class="well">
        <?php
        $a1=array("a"=>"bike1","b"=>"bike2","c"=>"bike3","d"=>"scooter");
        $a2=array("e"=>"bike1","f"=>"bike2","g"=>"bike3");

        $result=array_diff($a1,$a2);
        print_r($result);
        foreach ($result as $res){
            echo "<h1>$res</h1>";
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post