0

I want to convert a cursor to array so i see cursor result, for this when i used toArray of mongodb then this error shown

"Fatal error: Call to undefined method MongoCursor::toArray()"

Here is my code:

$getting_started_collection = $this->getServiceLocator()->get('Common\Collection\ResourcesGettingStarted');
$criteria = array(
    '$or' => array(
        array('affiliate_type' => 'cpl_cpm'), 
        array('affiliate_type' => 'cpl')
    )
);
$columns = array(
    '_id' => true,
    'title' => true,
    'description' => true,
    'logo' => true,
    'pdf' => true
);
$cursor = $getting_started_collection->fetchAll($criteria, $columns, true);
$data_array = $cursor->toArray();
echo("<pre>");
print_r($data_array);
die();

how i used https://docs.mongodb.com/manual/reference/method/cursor.toArray/ ?

Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56

1 Answers1

2

That's because MongoCursor class does not have method called toArray. Here is a list of all available methods- MongoCursor.
You should use iterator_to_array() as in Example #1 in manual:

<?php

$cursor = $collection->find();
var_dump(iterator_to_array($cursor));

?>

Source: http://php.net/manual/en/class.mongocursor.php

In your example:

$cursor = $getting_started_collection->fetchAll($criteria, $columns, true);
$data_array = iterator_to_array($cursor);
echo("<pre>");
print_r($data_array);
die();
SzymonM
  • 904
  • 8
  • 14