我想把一个游标转换成数组,这样我就可以看到游标的结果,所以当我使用mongodb的toArray时,会显示这个错误
“致命错误:调用未定义的方法MongoCursor::toArray()”
下面是我的代码:
$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();
我是如何使用https://docs.mongodb.com/manual/reference/method/cursor.toArray/的?
发布于 2017-03-29 19:34:42
这是因为MongoCursor
类没有名为toArray
的方法。下面是所有可用方法-- MongoCursor的列表。
您应该像手册中的Example #1
一样使用iterator_to_array()
:
<?php
$cursor = $collection->find();
var_dump(iterator_to_array($cursor));
?>
来源:http://php.net/manual/en/class.mongocursor.php
在您的示例中:
$cursor = $getting_started_collection->fetchAll($criteria, $columns, true);
$data_array = iterator_to_array($cursor);
echo("<pre>");
print_r($data_array);
die();
https://stackoverflow.com/questions/43091869
复制相似问题