dsfdfdfd6576578 2015-08-02 17:11
浏览 44

在php上显示评论

i want to display comments with php. My database is mongodb, and this is my collection articles :

{ "_id" : ObjectId("55be4a1a71dd7ecc05b7acd9"), "title" : "test", "content" : "test", "user" : "Paul", "saved_at" : ISODate("2015-08-02T16:49:30.480Z"), "comments" : [ { "comment" : "this is my test", "user" : "Paul" } ], { "comment" : "my second test", "user" : "Paul" } ]  }

So i want display this comments : "this is my test" and "my second test" i have this code but it don't work :

  <?php
       try {
           $mongodb = new MongoClient();
           $collection = $mongodb->blog->articles;
         } catch (MongoConnectionException $e) {
           die('Failed to connect to MongoDB '.$e->getMessage());
         }

         $query=array();
         $cursor=$collection->find($query);


         foreach($cursor as $doc){
             echo $doc['comments'];
         }



    ?>

so when i use this code i have this error :

Array to string conversion in line 86, its this line

echo $doc['comments'];

thanks with the help

  • 写回答

1条回答 默认 最新

  • dongyao4003 2015-08-02 17:15
    关注

    It's exactly as the error says - you're trying to print out an array, and echo can print only scalars (int, string, float, etc).

    To just quickly display the array, you could use var_dump(), but this should not be used in production. Instead, you could iterate over the comments like this:

    foreach($cursor as $doc)
    {
        foreach($docs['comments'] as $comment)
        {
            echo '"' . $comment['comment'] .'" by ' . $comment['user'];
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题