• 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
  • Ruby
  • 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 array_pop() – Remove Last Element from Array
  • 2.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 3.  php pi – Get Value of pi Using php pi() Function
  • 4.  How to Reverse Array in php Without a Function or Other Variables
  • 5.  How to Create an Empty Array in php
  • 6.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 7.  Get First Element of Array in php
  • 8.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 9.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 10.  php array_intersect() – Find Intersection of Two or More Arrays 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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy