阅读量:0
- 从数组中提取指定键的值组成新的数组:
$data = [ ['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob'], ['id' => 3, 'name' => 'Charlie'] ]; $names = array_column($data, 'name'); print_r($names);
- 从对象数组中提取指定属性的值组成新的数组:
class User { public $id; public $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } } $users = [ new User(1, 'Alice'), new User(2, 'Bob'), new User(3, 'Charlie') ]; $names = array_column($users, 'name'); print_r($names);
- 从关联数组中提取指定键的值组成新的数组:
$data = [ 'id' => 1, 'name' => 'Alice', 'age' => 25 ]; $values = array_values($data); print_r($values);