是的,PHP 集合(Collection)可以转换为数组使用。在 Laravel 中,集合是一个功能强大的工具,它提供了许多方便的方法来操作数据。要将集合转换为数组,您可以使用 toArray
方法。以下是一个示例:
use Illuminate\Support\Collection;
// 创建一个集合
$collection = new Collection([
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 28],
['name' => 'Tom', 'age' => 35],
]);
// 将集合转换为数组
$array = $collection->toArray();
// 打印数组
print_r($array);
输出结果:
Array
(
[0] => Array
(
[name] => John
[age] => 30
)
[1] => Array
(
[name] => Jane
[age] => 28
)
[2] => Array
(
[name] => Tom
[age] => 35
)
)
这样,您就可以将 PHP 集合转换为数组并在后续代码中使用数组了。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:php集合 能转换为对象吗