简体   繁体   中英

get mongoDB embedded document array value using php?

How to get the contact values?, and How are the values stored in php?

 {

 _id : "001",

 name : "fakename",

 contact_address : {

                    street : "12 Street",

                    city : "Cosmos",

                    contact : [ 
                               "123456789",
                               "012345678" 
                              ]

                   }
}

Query :

$cursor = $collection->find ( array('name' => 'fakename' ), array( 'contact_address.contact' ) );

   foreach ( $cursor as $doc ) {

         echo $doc[ 'contact_address' ][ 'contact' ];
   }

Result :

 Array

Motive : Intend to print the contact values.

To print array values you can use print_r , var_dump or var_export - for debugging purposes.

To iterate them and use values in other places - you can use for example foreach like that:

foreach ($array as $key => $value) {
    // $key holds the index, $value holds the value of the array
}

There are other ways to iterate array, in fact way too many in PHP, but this should suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM