如何在json对象中检索给定键的值?

时间:2014-10-19 02:34:07

标签: php json mongodb

我跟随this tutorial。我有这个指示:

$cursor = $collection->find(array("author" => "shreef"));
foreach ($cursor as $document) {
   print_r($document)
}

使用 print_r 显示正在返回的对象的结构。

Array
(
   [title] => cat with a hat
   [content] => once upon a time a cat with a hat ...
   [_id] => MongoId Object
           (
              [$id] => 4ea2213af7ede43c53000000
           )
)

如果我只想获取标题的值,该怎么办?

1 个答案:

答案 0 :(得分:1)

这样做你想要的吗?

$cursor = $collection->find(array("author" => "shreef"));
foreach ($cursor as $document) {
   echo $document['title'];
}