라이브러리

[PHP] MongoDBBSONDocument::offsetGet - 문서의 키 값을 반환합니다.




MongoDBBSONDocument::offsetGet

`MongoDBBSONDocument::offsetGet`은 MongoDBBSONDocument 클래스의 메서드 중 하나입니다. 이 메서드는 BSON 문서의 특정 키에 대한 값을 반환합니다.

Syntax

#hostingforum.kr
php

public mixed MongoDBBSONDocument::offsetGet(string $key)



Parameter

- `$key`: BSON 문서의 키 이름입니다.

Return Value

- `$value`: 키에 해당하는 값입니다. 만약 키가 존재하지 않으면 `null`을 반환합니다.

예제

#hostingforum.kr
php

use MongoDBBSONUTCDateTime;

use MongoDBBSONUTCDateTime;

use MongoDBBSONDocument;



// BSON 문서 생성

$document = new Document([

    'name' => 'John Doe',

    'age' => 30,

    'created_at' => new UTCDateTime(),

]);



// offsetGet 메서드 사용

echo $document->offsetGet('name') . "
"; // John Doe

echo $document->offsetGet('age') . "
"; // 30

echo $document->offsetGet('created_at') . "
"; // UTCDateTime Object

echo $document->offsetGet('non_existent_key') . "
"; // null



주의

- `offsetGet` 메서드는 BSON 문서의 키에 대한 값을 반환합니다. 만약 키가 존재하지 않으면 `null`을 반환합니다.
- `offsetGet` 메서드는 BSON 문서의 키 이름을 문자열로 전달해야 합니다.
- `offsetGet` 메서드는 BSON 문서의 키에 대한 값을 반환합니다. 만약 키가 배열의 키인 경우, 배열의 값이 반환됩니다.

추가 예제

#hostingforum.kr
php

// BSON 문서 생성

$document = new Document([

    'name' => 'Jane Doe',

    'age' => 25,

    'address' => [

        'street' => '123 Main St',

        'city' => 'Anytown',

        'state' => 'CA',

        'zip' => '12345',

    ],

]);



// offsetGet 메서드 사용

echo $document->offsetGet('name') . "
"; // Jane Doe

echo $document->offsetGet('age') . "
"; // 25

echo $document->offsetGet('address') . "
"; // ArrayObject Object

echo $document->offsetGet('address.street') . "
"; // 123 Main St

echo $document->offsetGet('non_existent_key') . "
"; // null



이 예제에서는 BSON 문서의 키가 배열의 키인 경우, 배열의 값이 반환되는 것을 볼 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 408 페이지

검색

게시물 검색