php8.5
Home/ Manual/ bson / packedarray/ MongoDB\BSON\PackedArray::fromJSON

MongoDB\BSON\PackedArray::fromJSON

PHP function Edit on GitHub ✎

(mongodb >=1.20.0)

Construct a new BSON array instance from a JSON string

Description

MongoDB\BSON\PackedArray::fromJSON(string $json): MongoDB\BSON\PackedArray

Converts an extended JSON string to its BSON representation.

Parameters

json (string)

JSON value to be converted.

Return Values

Returns a new MongoDB\BSON\PackedArray instance.

Errors/Exceptions

  • Throws MongoDB\Driver\Exception\UnexpectedValueException if the JSON value cannot be converted to a BSON array (e.g. due to a syntax error).

Examples

MongoDB\BSON\PackedArray::fromJSON example

php
<?php

$json = '[ "foo", { "$numberInt" : "123" }, { "$numberLong" : "4294967295" }, { "$oid" : "56315a7c6118fd1b920270b1" } ]';
$packedArray = MongoDB\BSON\PackedArray::fromJSON($json);
var_dump($packedArray);

?>

The above example will output:

output
object(MongoDB\BSON\PackedArray)#1 (2) {
  ["data"]=>
  string(68) "MQAAAAIwAAQAAABmb28AEDEAewAAABIyAP////8AAAAABzMAVjFafGEY/RuSAnCxAA=="
  ["value"]=>
  array(4) {
    [0]=>
    string(3) "foo"
    [1]=>
    int(123)
    [2]=>
    int(4294967295)
    [3]=>
    object(MongoDB\BSON\ObjectId)#2 (1) {
      ["oid"]=>
      string(24) "56315a7c6118fd1b920270b1"
    }
  }
}

See Also

  • MongoDB\BSON\PackedArray::fromPHP
  • MongoDB Extended JSON
  • BSON Types

Source: reference/mongodb/bson/packedarray/fromjson.xml · from the official PHP manual (php/doc-en)