Current Location: Home> Latest Articles> How to Filter Arrays in PHP Using array_diff() Function

How to Filter Arrays in PHP Using array_diff() Function

M66 2025-06-06

In PHP, array_diff() is a very useful function that helps us eliminate elements from other arrays from an array. This function is often used in scenarios where some unwanted elements are removed from an array or perform differential operations between arrays. Today, let’s learn how to easily exclude specific elements in an array using the array_diff() function.

Introduction to array_diff() function

The array_diff() function is used to compare two or more arrays, returning an array containing all values ​​that exist in the first array but not in other arrays. Simply put, it can help us find out the difference between an array and other arrays.

The basic syntax of a function is as follows:

 array_diff(array $array1, array $array2, array ...$arrays): array
  • $array1 : The first array to be compared.

  • $array2, ...$arrays : One or more arrays that will be used to compare with $array1 .

Example: Exclude specific elements in an array

Suppose we have an array with multiple elements, and we want to exclude some specific elements from it. For example, suppose we have a shopping cart list of items, but we want to exclude some items that have been sold out. We can use array_diff() to easily implement this function.

Sample code:

 <?php
// Original array:List of items in the shopping cart
$cartItems = ["apple", "banana", "orange", "grape", "pineapple"];

// The array of items we want to exclude:Sold out items
$soldOutItems = ["banana", "grape"];

// use array_diff() 函数排除Sold out items
$availableItems = array_diff($cartItems, $soldOutItems);

// Print the excluded product list
print_r($availableItems);
?>

Results output:

 Array
(
    [0] => apple
    [2] => orange
    [4] => pineapple
)

In this example, the array_diff() function compares the cartItems array with the soldOutItems array and returns a new array availableItems that contains all unsold items.

Things to note

  1. Indexing problem : array_diff() will retain the key names in the original array. If you need to re-index the array, you can use the array_values() function, for example:

     $availableItems = array_values(array_diff($cartItems, $soldOutItems));
    
  2. Type comparison : array_diff() uses strict comparison, which means it distinguishes data types. If there is a string "1" in array1 and an integer 1 in array2 , they will be considered different elements.

Use array_diff() function in URL

Suppose you have an array of multiple URLs, from which you want to exclude certain URLs (such as links that have expired). You can easily achieve this with the array_diff() function. Here is a code that uses a URL example:

 <?php
// original URL Array
$urls = [
    "https://www.example.com/page1",
    "https://www.example.com/page2",
    "https://www.example.com/page3",
    "https://www.example.com/page4"
];

// Need to be excluded URL Array
$urlsToExclude = [
    "https://www.example.com/page2",
    "https://www.example.com/page4"
];

// use array_diff() Exclude specific URL
$filteredUrls = array_diff($urls, $urlsToExclude);

// Revise URL The domain name is m66.net
$updatedUrls = array_map(function ($url) {
    return preg_replace('/https:\/\/www\.[a-z]+\.[a-z]+/', 'https://m66.net', $url);
}, $filteredUrls);

// Print excluded URL List
print_r($updatedUrls);
?>

Results output:

 Array
(
    [0] => https://m66.net/page1
    [2] => https://m66.net/page3
)

Through this example, we not only exclude unwanted URLs, but also change the domain name of the URL to m66.net through the preg_replace() function.

Summarize

By using PHP's array_diff() function, we can easily exclude specific elements from an array. In actual development, this method can be applied to a variety of scenarios, such as filtering unnecessary data, removing duplicates, etc. Hopefully this article can help you better understand how to use array_diff() to process elements in an array.