In PHP, the array_column function is use
▥php
𝄐 0
php array_column
In PHP, the array_column function is used to fetch the values from a single column of an array. The function takes three arguments: the input array, the column key (or index) from which to fetch the values, and an optional index key to use as the returned array's index. Here's an example to demonstrate its usage:
php
$users = [
['id' => 1, 'name' => 'John', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'age' => 30],
['id' => 3, 'name' => 'Bob', 'age' => 40]
];
// Example 1: Fetching a single column
$names = array_column($users, 'name');
print_r($names); // Outputs: Array ( [0] => John [1] => Jane [2] => Bob )
// Example 2: Fetching a column with an index
$ages = array_column($users, 'age', 'id');
print_r($ages); // Outputs: Array ( [1] => 25 [2] => 30 [3] => 40 )
In Example 1, the array_column($users, 'name') function fetches the 'name' column from the $users array and returns an array containing only the names.
In Example 2, the array_column($users, 'age', 'id') function fetches the 'age' column from the $users array, but also uses the 'id' column as the index for the returned array.
Note that the array_column function was introduced in PHP 5.5.0, so make sure you are using a compatible version.
In PHP, the array_column function is used to fetch the values from a single column of an array. The function takes three arguments: the input array, the column key (or index) from which to fetch the values, and an optional index key to use as the returned array's index. Here's an example to demonstrate its usage:
php
$users = [
['id' => 1, 'name' => 'John', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'age' => 30],
['id' => 3, 'name' => 'Bob', 'age' => 40]
];
// Example 1: Fetching a single column
$names = array_column($users, 'name');
print_r($names); // Outputs: Array ( [0] => John [1] => Jane [2] => Bob )
// Example 2: Fetching a column with an index
$ages = array_column($users, 'age', 'id');
print_r($ages); // Outputs: Array ( [1] => 25 [2] => 30 [3] => 40 )
In Example 1, the array_column($users, 'name') function fetches the 'name' column from the $users array and returns an array containing only the names.
In Example 2, the array_column($users, 'age', 'id') function fetches the 'age' column from the $users array, but also uses the 'id' column as the index for the returned array.
Note that the array_column function was introduced in PHP 5.5.0, so make sure you are using a compatible version.
本文地址:
/show-279808.html
版权声明:除非特别标注原创,其它均来自互联网,转载时请以链接形式注明文章出处。