Free cookie consent management tool by TermsFeed Generator PHP Array Diff | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Array Diff

PHP Array Diff

Last Updated on Feb 14, 2023

Introduction

If you want to find the items in your array that are not present in the other arrays, array diff is your friend.

Here we have 3 arrays:

$people = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver',
   'saas'     => 'simon'
];
$peopleA = [
   'php'      => 'amir updated',
   'feedhive' => 'simon',
   'twitter'  => 'pratham updated',
];
$peopleB = [
   'twitter'  => 'pratham updated',
   'docker'   => 'francesco updated',
   'web3'     => 'oliver'
];

Array Diff

we want to find the items in people array that are not present in peopleA and peopleB arrays.

There are 3 different array diff functions and we should choose them based on what we want to compare.

array_diff if we want to compare based on values

array_diff_key if we want to compare based on keys

array_diff_assoc if we want to compare based on keys and values

Let’s see what is the result of each on of them

$people = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver',
   'saas'     => 'simon'
];
$peopleA = [
   'php'      => 'amir updated',
   'feedhive' => 'simon',
   'twitter'  => 'pratham updated',
];
$peopleB = [
   'twitter'  => 'pratham updated',
   'docker'   => 'francesco updated',
   'web3'     => 'oliver'
];
array_diff($people,$peopleA,$peopleB);
// Array (
//      [php]       => amir
//      [twitter]   => pratham
//      [docker]    => francesco
// )
array_diff_key($people,$peopleA,$peopleB);
// Array (
//      [saas] => simon
// )
array_diff_assoc($people,$peopleA,$peopleB);
// Array (
//      [php]       => amir
//      [twitter]   => pratham
//      [docker]    => francesco
//      [saas]      => simon
// )

Conclusion

Now you know about array diff function in PHP.

I recommend you to open a PHP files and try to define multiple arrays. then try to find the values are different in the arrays.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • array diff function in PHP
  • array diff based on keys
  • array diff based on values
  • array diff based on keys and values

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP isset and empty
Mar 21, 2023 programming

PHP isset and empty

Today we are going to learn about isset and empty in PHP. isset and empty are two functions that help us check the existence of a value. They are way more useful than you think. ...

5 Min Read Read More
PHP Constant
Mar 22, 2023 programming

PHP Constant

Today we are going to talk about constants in PHP. Constants are like variables that can’t be changed. Hence the name! it’s constant and it’s not variable. they are a good way to store information. ...

4 Min Read Read More
A Beginner's Guide to Different Types of Software Testing
Apr 07, 2024 programming

A Beginner's Guide to Different Types of Software Testing

Software testing is a crucial aspect of programming and software development. By testing, developers can ensure that their software performs as expected. ...

9 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...