函数名:MongoDB\BSON\Iterator::__construct()
适用版本:PHP 5.6.0 及以上版本
用法:该函数用于创建 MongoDB\BSON\Iterator 对象的实例。
语法:
public MongoDB\BSON\Iterator::__construct ( array|object $data )
参数:
- $data: 一个包含 BSON 数据的数组或对象。
返回值:无返回值。
示例: 以下示例展示了如何使用 MongoDB\BSON\Iterator::__construct() 函数创建一个 MongoDB\BSON\Iterator 对象的实例,并遍历该对象中的数据。
<?php
// 创建一个包含 BSON 数据的数组
$data = [
[
'_id' => new MongoDB\BSON\ObjectID,
'name' => 'John Doe',
'age' => 30,
],
[
'_id' => new MongoDB\BSON\ObjectID,
'name' => 'Jane Smith',
'age' => 25,
],
];
// 创建 MongoDB\BSON\Iterator 对象
$iterator = new MongoDB\BSON\Iterator($data);
// 遍历 MongoDB\BSON\Iterator 对象中的数据
foreach ($iterator as $document) {
echo $document['_id'], ': ', $document['name'], ' (', $document['age'], ")\n";
}
?>
输出:
5e7e5b0d8b6b9a001b5b4a2c: John Doe (30)
5e7e5b0d8b6b9a001b5b4a2d: Jane Smith (25)
注意:在使用该函数之前,确保已经安装了 MongoDB 扩展,并且启用了对应的驱动程序。