title: MongoDB (php) - return document property as an array instead of multiple properties
body: <p>I am reading a document from a mongodb database and passing it with php to client side.</p>
  
  <p>The document contains an array property. The problem is that the client side receives it as an object with properties with the names <code>0</code>,<code>1</code> and so on, instead of a standard array.</p>
  
  <p>This is the original data:</p>
  
  <pre><code>{ 
      "_id" : ObjectId("573b47a1f99a8a1f9a6278a5"), 
      "persons" : [
          {
              "name" : "Moshe",
          }, 
          {
              "name" : "E",
          }, ...
      ]
  }
  </code></pre>
  
  <p>As requested, I am attaching the var_export:</p>
  
  <pre><code>array (
    0 =&gt; 
    MongoDB\Model\BSONDocument::__set_state(array(
       '_id' =&gt; 
      MongoDB\BSON\ObjectID::__set_state(array(
      )),
       'persons' =&gt; 
      MongoDB\Model\BSONArray::__set_state(array(
         0 =&gt; 
        MongoDB\Model\BSONDocument::__set_state(array(
           'name' =&gt; 'Moshe',
        )),
         1 =&gt; 
        MongoDB\Model\BSONDocument::__set_state(array(
           'name' =&gt; 'E',
        )),
      )),
    )),
  )
  </code></pre>
  
  <p>And var_dump:</p>
  
  <pre><code>array(1) {
    [0]=&gt;
    object(MongoDB\Model\BSONDocument)#40 (1) {
      ["storage":"ArrayObject":private]=&gt;
      array(2) {
        ["_id"]=&gt;
        object(MongoDB\BSON\ObjectID)#11 (1) {
          ["oid"]=&gt;
          string(24) "573b47a1f99a8d1f986278a5"
        }
        ["persons"]=&gt;
        object(MongoDB\Model\BSONArray)#34 (1) {
          ["storage":"ArrayObject":private]=&gt;
          array(2) {
            [0]=&gt;
            object(MongoDB\Model\BSONDocument)#10 (1) {
              ["storage":"ArrayObject":private]=&gt;
              array(1) {
                ["name"]=&gt;
                string(5) "Moshe"
              }
            }
            [1]=&gt;
            object(MongoDB\Model\BSONDocument)#12 (1) {
              ["storage":"ArrayObject":private]=&gt;
              array(1) {
                ["name"]=&gt;
                string(1) "E"
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>This is the PHP code(all of it):</p>
  
  <pre><code>function select(){
      $conn = new MongoDB\Client("mongodb://localhost:27017");
      $db = $conn-&gt;mydb;
      $cursor = $db-&gt;entries_meta_data-&gt;find();
      return current($cursor-&gt;toArray());
  }
  </code></pre>
  
  <p>Then i pass the object to the client with a json_encode like this:</p>
  
  <pre><code>echo json_encode(select());
  </code></pre>
  
  <p>And the result as it appears in the client side is:</p>
  
  <pre><code>{ 
      "_id" : ObjectId("573b47a1f99a8a1f9a6278a5"), 
      "persons" : {
          "0" : {
              "name" : "Moshe",
          }, 
          "1" : {
              "name" : "E",
          }, ...
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  LordNeo actually solved it. After reading his answer i have changed the last line in my "select" function to the following:</p>
  
  <pre><code>return json_decode(json_encode(current($cursor-&gt;toArray()),true);
  </code></pre>
  
  <p>It looks horrible, but it works.</p>
  
  <p>I will be more than happy to hear a better solution.</p>