Hướng dẫn array_diff in php

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm array_diff() trong PHP

Hướng dẫn cách sử dụng hàm array_diff() về mảng trong lập trình PHP

Tác dụng của hàm array_diff()

The array_diff() function compares the values of two or more arrays and returns the differences.

The following table summarizes the technical details of this function.

Return Value:Returns an array containing all the elements from array1 that are not present in any of the other arrays.
Version:PHP 4.0.1+


Syntax

The basic syntax of the array_diff() function is given with:

array_diff(array1, array2, ...);

The following example shows the array_diff() function in action.


Parameters

The array_diff() function accepts the following parameters.

ParameterDescription
array1 Required. Specifies the array to compare from.
array2 Required. Specifies an array to compare against.
... Optional. Specifies more arrays to compare against.


More Examples

Here're some more examples showing how array_diff() function actually works:

The following example shows how to use this function to compare an array against two other arrays.

Two elements are considered equal if their string representation are same, i.e., (string) $elem1 === (string) $elem2. Let's take a look at the following example:

You can also use associative arrays, however the keys are not considered in the comparison.

"red", "b"=>"green", "c"=>"blue", "d"=>"yellow");
$array2 = array("x"=>"black", "y"=>"blue", "z"=>"red");

// Computing the difference
$result = array_diff($array1, $array2);
print_r($result);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm array_diff() trong PHP

Hướng dẫn cách sử dụng hàm array_diff() về mảng trong lập trình PHP

Nội dung chính

  • Tác dụng của hàm array_diff()
  • More Examples
  • Bài viết này đã giúp ích cho bạn?
  • Bài viết mới
  • Tác dụng của hàm array_diff()
  • More Examples
  • Bài viết này đã giúp ích cho bạn?
  • Bài viết mới

Tác dụng của hàm array_diff()

The array_diff() function compares the values of two or more arrays and returns the differences.

The following table summarizes the technical details of this function.

Return Value:Returns an array containing all the elements from array1 that are not present in any of the other arrays.
Version:PHP 4.0.1+

Syntax

The basic syntax of the array_diff() function is given with:

array_diff(array1, array2, ...);

The following example shows the array_diff() function in action.


Parameters

The array_diff() function accepts the following parameters.

ParameterDescription
array1 Required. Specifies the array to compare from.
array2 Required. Specifies an array to compare against.
... Optional. Specifies more arrays to compare against.

More Examples

Here're some more examples showing how array_diff() function actually works:

The following example shows how to use this function to compare an array against two other arrays.

Two elements are considered equal if their string representation are same, i.e., (string) $elem1 === (string) $elem2. Let's take a look at the following example:

You can also use associative arrays, however the keys are not considered in the comparison.

"red", "b"=>"green", "c"=>"blue", "d"=>"yellow");
$array2 = array("x"=>"black", "y"=>"blue", "z"=>"red");

// Computing the difference
$result = array_diff($array1, $array2);
print_r($result);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm array_diff() trong PHP

Hướng dẫn cách sử dụng hàm array_diff() về mảng trong lập trình PHP

Tác dụng của hàm array_diff()

The array_diff() function compares the values of two or more arrays and returns the differences.

The following table summarizes the technical details of this function.

Return Value:Returns an array containing all the elements from array1 that are not present in any of the other arrays.
Version:PHP 4.0.1+

Syntax

The basic syntax of the array_diff() function is given with:

array_diff(array1, array2, ...);

The following example shows the array_diff() function in action.


Parameters

The array_diff() function accepts the following parameters.

ParameterDescription
array1 Required. Specifies the array to compare from.
array2 Required. Specifies an array to compare against.
... Optional. Specifies more arrays to compare against.

More Examples

Here're some more examples showing how array_diff() function actually works:

The following example shows how to use this function to compare an array against two other arrays.

Two elements are considered equal if their string representation are same, i.e., (string) $elem1 === (string) $elem2. Let's take a look at the following example:

You can also use associative arrays, however the keys are not considered in the comparison.

"red", "b"=>"green", "c"=>"blue", "d"=>"yellow");
$array2 = array("x"=>"black", "y"=>"blue", "z"=>"red");

// Computing the difference
$result = array_diff($array1, $array2);
print_r($result);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới