• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / php array_diff() – Find Elements of Array Not Present in Other Arrays

php array_diff() – Find Elements of Array Not Present in Other Arrays

April 5, 2022 Leave a Comment

The php array_diff() function allows us to find the elements of an array which are not in all of the other given arrays.

$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);

print_r(array_diff($array1,$array2));

// Output:
Array
(
    [0] => 5
    [2] => 8
)

When working with multiple arrays, it can be useful to find the entire collection of elements which are only in one array. The difference of one array with one or more arrays is the collection of elements which are only in the first array.

So, for example, if we have an array A and an array B, then the difference of A and B is a collection of elements which are in A but not in b.

We can get the difference of two arrays in php easily with the php array_diff() function.

Below is a simple example of how to use array_diff() to find the difference of two arrays in php.

$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);

print_r(array_diff($array1,$array2));

// Output:
Array
(
    [0] => 5
    [2] => 8
)

Finding the Difference of Three or More Arrays with array_diff() in php

The php array_diff() function can find the difference of more than two arrays.

The examples above only found the difference between two arrays, but we can find the difference of three or more arrays just as easy.

Below is an example of how to use array_diff() to find the differenec of three arrays in php.

Note, that array_diff() only looks at the first array, and is not finding the difference between the second or third array.

$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);
$array3 = array(0,9,8,1,3);

print_r(array_diff($array1,$array2, $array3));

// Output:
Array
(
    [0] => 5
)

Hopefully this article has been useful for you to learn how to use the array_diff() function in php.

Other Articles You'll Also Like:

  • 1.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 2.  Get Query String from URL in php
  • 3.  Remove String from String in php with str_replace()
  • 4.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 5.  Creating Custom category.php With Pagination
  • 6.  php is_numeric – Check if Variable is a Number
  • 7.  php Delete Files with php Unlink Function
  • 8.  php atan – Find Arctangent and Inverse Tangent of Number
  • 9.  php print_r() Function- Get Information about Variables in php
  • 10.  How to Add Items to Array in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy