查詢

MongoDB\BSON\Iterator::__construct()函式—用法及示例

「 建立 MongoDB\BSON\Iterator 物件的例項 」


函式名: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 擴充套件,並且啟用了對應的驅動程式。

補充糾錯
熱門PHP函式
分享連結