Hướng dẫn dùng array_intersect_key trong PHP

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm array_intersect_key[] trong PHP

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

Tác dụng của hàm array_intersect_key[]

The array_intersect_key[] function compares the keys of two or more arrays and returns the matches. The keys values are not considered in the comparison, only the keys are checked.

The following table summarizes the technical details of this function.

Return Value:Version:
Returns an array containing all the elements from array1 whose keys are present in all of the other arrays.
PHP 5.1.0+

Syntax

The basic syntax of the array_intersect_key[] function is given with:

array_intersect_key[array1, array2, ...];

The following example shows the array_intersect_key[] function in action.

Parameters

The array_intersect_key[] function accepts the following parameters.

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

More Examples

Here're some more examples showing how array_intersect_key[] function actually works:

In the following example this function compares an array against two other arrays.

The two keys from the key=>value pairs are considered equal if their string representation are same, i.e., [string] $key1 === [string] $key2. Let's try out the following example:

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

Bài viết mới

Chủ Đề