0

I have the following code:

<?php

require __DIR__ . '/vendor/autoload.php';

$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->bbcData->programmeData;
$result = $collection->find( [ '_id' => 'b007jwd9'] );

foreach ($result as $entry) {
    echo $entry['_id'], ': ', $entry['complete_title'], "\n";
}

?>

The JSON is the following:

{"complete_title":{"name":"wimsey - murder must advertise","episode":"sudden decease of a man in dress clothes"},"media_type":"audio","_id":"b007jwd9","categories":{"category1":["drama", "crime"]}}

I want to print out the name of the complete_title however the docs do not show the syntax for this. Currently I'm getting an error saying the complete_title is an BSONDocument so it can not be converted into a String.

kulsoompatel
  • 220
  • 1
  • 3
  • 18

1 Answers1

0

Use iterator_to_array to get array.

foreach ($result as $entry) {
    $array = iterator_to_array($entry['complete_title']);
    echo $entry['_id'], ': ', $array['name'], "\n";
}
s7vr
  • 73,656
  • 11
  • 106
  • 127