我怎样才能将mongodb数组(简单和嵌入数组)元素用作php索引数组

时间:2017-06-16 08:55:57

标签: php mongodb

我想逐个打印所有数组和嵌入式数组元素......

Db结构:

  Users: [
        { 
           "_id" : "1", 
           "uname" : "Eminem", 
           "pro_img" : [ "img1", "img2", "img3" ]   // this values
           "gener" : "rap",
           "about" :
                [
                  { "child_name" : ["child1", "child2" , "child3"] },   // and this values
                  {"dob" : "15-07-1970"}

                ] 
         },

        { 
          "_id" : "2", 
          "uname" : "Armin", 
          "pro_img" : [ "img21", "img23", "img34" ]
        }
]

我按照以下方式编写代码

<?php 
      include 'vendor/dbcon.php';
      $documentlist = $Users-> find(
                       ['uname' => "Eminem"],
                       ['projection' =>  ['_id' => 0, 'pro_img' => 1]]
                    );
        foreach ($documentlist as $doc) {
        echo var_dump($doc);
    }
?>

输出

object(MongoDB\Model\BSONDocument)#14 (1) { 
   ["storage":"ArrayObject":private]=> array(1) { 
   ["pro_img"]=> object(MongoDB\Model\BSONArray)#10 (1) { 
   ["storage":"ArrayObject":private]=> array(3) { 
   [0]=> string(4) "img1" [1]=> string(4) "img2" [2]=> string(4) "img3" } } } }

但我想以下列方式存储:

$info= array("img1", "img2", "img3");

同样条件的嵌入值[&#39; uname&#39; =&GT; &#34;阿姆&#34;]

$info= array("child1", "child2", "child3");

这样我就可以将它用作一个简单的php数组...

0 个答案:

没有答案