Hướng dẫn dùng column array trong PHP
# DescriptionHà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 đó. Show Nội dung chính Show
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức. # ParametersCú pháp : array array_column ( array $array , mixed $column_key [, mixed $index_key = null ] ) Trong đó :
# Return valuesTrả 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] # ExamplesSau đâ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_keyCode: $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_keyCode: // 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!
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 The following table summarizes the technical details of this function.
SyntaxThe basic syntax of the sort(array, column_key, index_key); The following example shows the
ParametersThe
More ExamplesHere're some more examples showing how 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.
The following example get the column of names from the public "name" property of an object.
Bài viết này đã giúp ích cho bạn?Bài viết mới |