
- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Environment Setup
- PHP - Syntax Overview
- PHP - Variable Types
- PHP - Constants
- PHP - Operator Types
- PHP - Decision Making
- PHP - Loop Types
- PHP - Arrays
- PHP - Strings
- PHP - Web Concepts
- PHP - GET & POST
- PHP - File Inclusion
- PHP - Files & I/O
- PHP - Functions
- PHP - Cookies
- PHP - Sessions
- PHP - Sending Emails
- PHP - File Uploading
- PHP - Coding Standard
- Advanced PHP
- PHP - Predefined Variables
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Bugs Debugging
- PHP - Date & Time
- PHP & MySQL
- PHP & AJAX
- PHP & XML
- PHP - Object Oriented
- PHP - For C Developers
- PHP - For PERL Developers
- PHP Form Examples
- PHP - Form Introduction
- PHP - Validation Example
- PHP - Complete Form
- PHP login Examples
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP AJAX Examples
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML Example
- PHP - XML Introduction
- PHP - Simple XML
- PHP - Simple XML GET
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Frame Works
- PHP - Frame Works
- PHP - Core PHP vs Frame Works
- PHP Design Patterns
- PHP - Design Patterns
- PHP Function Reference
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Useful Resources
- PHP - Discussion
PHP array_diff() Function
Definition and Usage
The array_diff() function compares array1 against one or more other arrays passed to it and returns the values in array1 that are not present in any of the other arrays.
Syntax
array array_diff ( array $array1, array $array2 [, array $array3 ...] );
Parameters
Sr.No | Parameter & Description |
---|---|
1 |
array1 (Required) This is the first array which will be compared with other arrays passed to the function. |
2 |
array2 (Required) This is an array to be compared with the first array |
3 |
array3 (Optional) This is the second array to be compared with the first array |
4 |
More Arrays (Optional) You can pass more number of arrays you want to compare with the first input array. |
Return Values
The PHP function array_diff() returns an array containing all the entries from input array array1 which which are not present in any of the other arrays passed to the function.
PHP Version
This function was first introduced in PHP Version 4.0.1.
Example
Try out following example −
<?php $array1 = array("orange", "banana", "apple"); $array2 = array("orange", "mango", "apple"); print_r(array_diff($array1, $array2)); ?>
This will produce the following result −
Array ( [1] => banana )
Example
Multiple occurrences in $array1 are all treated the same way. Try out following example −
<?php $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); print_r(array_diff($array1, $array2)); ?>
This will produce the following result −
Array ( [1] => blue )