# Description
Hàm array_column[] trong php dùng để lấy một cột trong một mảng và trả về giá trị từ một cột duy nhất đó.
Nội dung chính Show
- # Description
- # Parameters
- # Return values
- Trường hợp không có $index_key
- Trường hợp có $index_key
- Tác dụng của hàm array_column[]
- More Examples
- Bài viết này đã giúp ích cho bạn?
- Bài viết mới
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.
# Parameters
Cú pháp : array array_column [ array $array , mixed $column_key [, mixed $index_key = null ] ]
Trong đó :
- $array : mảng đa chiều , nói một cách đơn giản nó là 1 mảng chứa nhiều thành phần mảng khác trong đó [ bắt buộc phải có ]
- $column_key : cột các giá trị trả về . Mặc định các giá trị này có key là số nguyên hoặc có thể tùy chỉnh bằng $index_key [ bắt buộc phải có ]
- $index_key : là key của giá trị trả về, tham số này nếu không truyền vào thì sẽ trả về mảng trả về sẽ có key sex bắt đầu bằng 0, ngược lại key sẽ là giá trị của cột mà ta chỉ định
# Return values
Trả về một mảng có các giá trị đại diện cho một cột duy nhất từ mảng đầu vào
Bài viết này được đăng tại [free tuts .net]
# Examples
Sau đây là ví dụ cơ bản về trường hợp có $index_key và không có $index_key
Trường hợp không có $index_key
Code:
$records = array[ array[ 'id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe', ], array[ 'id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith', ], array[ 'id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones', ], array[ 'id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe', ] ]; $first_names = array_column[$records, 'first_name']; print_r[$first_names];
Result :
Array [ [0] => John [1] => Sally [2] => Jane [3] => Peter ]
Trường hợp có $index_key
Code:
// Sử dụng $records trong ví dụ trên $last_names = array_column[$records, 'last_name', 'id']; print_r[$last_names];
Result:
Array [ [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe ]
No more examples!
- Trang chủ
- Phát triển web
- PHP
- Hàm array_column[] trong PHP
Hướng dẫn cách sử dụng hàm array_column[] về mảng trong lập trình PHP
Tác dụng của hàm array_column[]
The array_column[]
function return the values from a single column in the input array.
The following table summarizes the technical details of this function.
Returns an array of values representing a single column from the input array. |
Since PHP 7.0.0, an array of objects can also be used. |
PHP 5.5.0+ |
Syntax
The basic syntax of the array_column[]
function is given with:
sort[array, column_key, index_key];
The following example shows the array_column[]
function in action.
"1",
"name" => "Titanic",
"genre" => "Drama",
],
array[
"id" => "2",
"name" => "Justice League",
"genre" => "Action",
],
array[
"id" => "3",
"name" => "Joker",
"genre" => "Thriller",
]
];
// Getting the column of names
$names = array_column[$movies, "name"];
print_r[$names];
?>
Parameters
The array_column[]
function accepts the following parameters.
array | Required. Specifies a multi-dimensional array or an array of objects to work on. |
column_key | Required. Specifies the index or key name of the column you want to retrieve. This parameter can also be |
index_key | Optional. Specifies the column to use as the index/keys for the returned array. |
More Examples
Here're some more examples showing how array_column[]
function actually works:
The following example demonstrates how to retrieve the "name" column values from the movies array indexed by the "id" column values. You can choose any column for indexing purpose.
"1",
"name" => "Titanic",
"genre" => "Drama",
],
array[
"id" => "2",
"name" => "Justice League",
"genre" => "Action",
],
array[
"id" => "3",
"name" => "Joker",
"genre" => "Thriller",
]
];
// Getting the column of names
$names = array_column[$movies, "name", "id"];
print_r[$names];
?>
The following example get the column of names from the public "name" property of an object.
name = $name;
}
}
// Creating the array of objects
$persons = [
new Person["Peter"],
new Person["Alice"],
new Person["Clark"],
];
// Getting the column of names
$names = array_column[$persons, "name"];
print_r[$names];
?>