AppDividend
Latest Code Tutorials

PHP array_udiff() Function Example | array_udiff() Method In PHP

0

PHP array_udiff() Function Example | array_udiff() Method In PHP is today’s topic. The array_udiff() function compares the values of two or more arrays and returns the differences. This function uses the user-defined function to compare the values! PHP array_udiff() function compares the values of two (or more) arrays, and return the array that contains the elements from array1 that are not present in the array2 or array3, etc.

PHP array_udiff() Function

The array_udiff() is the inbuilt function and used for distinguishing between the two or more arrays.

The array_udiff() function compares the different values of two or more arrays by use of user-defined function data comparison and returns the differences.

array_udiff(array1, array2, array3, ..., myfunction)

The array1 parameter is required. The array to compare from.

The array2 parameter is required and an array to compare against.

The array3 parameter is Optional and more arrays to compare against.

The myfunction is required, and a string that defines the callable comparison function. The comparison function must return the integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Example

<?php
  
  function compare($a, $b)
  {
    if ($a===$b)
    {
      return 0;
    }
    return ($a > $b) ? 1 : -1;
  }
  
  $a1 = array("e" => "Eleven", "m" => "Millie Bobby Brown", "n" => "Noah Schnapp");
  $a2 = array("el" => "Eleven", "f" => "Finn Wolfhard", "c" => "Caleb Mclaughlin");
  
  $op = array_udiff($a1, $a2, "compare");
  print_r($op);

See the output.

➜  pro php app.php
Array
(
    [m] => Millie Bobby Brown
    [n] => Noah Schnapp
)
➜  pro

The $a1 contains only one values which are not present in $a2.

Take four array (array1, array2, array3, and array4) and using user-defined key comparison function array_udiff().

<?php
  
  function compare($a, $b)
  {
    if ($a===$b)
    {
      return 0;
    }
    return ($a > $b) ? 1 : -1;
  }
  
  $a1 = array("e" => "Eleven", "m" => "Millie Bobby Brown", "n" => "Noah Schnapp");
  $a2 = array("el" => "Eleven", "f" => "Finn Wolfhard", "c" => "Caleb Mclaughlin");
  $a3 = array("d" => "David Harbour", "w" => "Winona Ryder", "j" => "Joe Keary");
  $a4 = array("j" => "Eleven", "f" => "Finn Wolfhard", "g" => "Gaten Matarazzo");
  
  $op = array_udiff($a1, $a2, $a3, $a4, "compare");
  print_r($op);

See the output.

➜  pro php app.php
Array
(
    [m] => Millie Bobby Brown
    [n] => Noah Schnapp
)
➜  pro

Take same Arrays

Let’s take two arrays whose values are the same.

<?php
  
  function compare($a, $b)
  {
    if ($a===$b)
    {
      return 0;
    }
    return ($a > $b) ? 1 : -1;
  }
  
  $a1 = array("e" => "Eleven", "m" => "Millie Bobby Brown", "n" => "Noah Schnapp");
  $a2 = array("e" => "Eleven", "m" => "Millie Bobby Brown", "n" => "Noah Schnapp");

  $op = array_udiff($a1, $a2, "compare");
  print_r($op);

See the output.

➜  pro php app.php
Array
(
)
➜  pro

Custom Callback Function

You can use and array_udiff define your comparison callback. I assume that both arrays have precisely the same structure.

The callback function must return the negative integer if the first argument is less than the second argument; the positive number if it’s bigger; or 0 if it’s equal.

Then, you can return any number different to 0 to show that the parameters are different and 0 if they are equal.

Finally, PHP array_udiff() Function Example | array_udiff() Method In PHP is over.

Recommended Posts

PHP array_product() Function

How To Reverse Array In PHP

How To Sort Array By Key in PHP

PHP Sorting Arrays Tutorial

How To Convert PHP Object To Associative Array

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.