ArrayColumn
是 PHP 中的一个内置函数,它用于从二维数组中提取某一列的所有值
$users = [
['name' => 'Alice', 'email' => 'alice@example.com'],
['name' => 'Bob', 'email' => 'bob@example.com'],
['name' => 'Charlie', 'email' => 'charlie@example.com'],
];
$emails = ArrayColumn($users, 'email');
$users = [
['name' => 'Alice', 'age' => 30],
['name' => 'Bob', 'age' => 25],
['name' => 'Charlie', 'age' => 22],
];
$usersJson = json_encode(ArrayColumn($users, 'age'));
$users = [
['name' => 'Alice', 'age' => 30],
['name' => 'Bob', 'age' => 25],
['name' => 'Charlie', 'age' => 22],
];
$adults = array_filter($users, function ($user) {
return $user['age'] >= 18;
}, ARRAY_FILTER_USE_BOTH);
$users = [
['name' => 'Alice', 'age' => 30],
['name' => 'Bob', 'age' => 25],
['name' => 'Charlie', 'age' => 22],
];
$usersStrings = array_map(function ($user) {
return ['name' => $user['name'], 'age' => (string)$user['age']];
}, $users);
总之,ArrayColumn
在 PHP 开发中的应用场景非常广泛,它可以帮助您更轻松地处理和分析二维数组数据。