MongoDB PHP:如何使用JSON feed获取ObjectId? (它是空白的) [英] MongoDB PHP: How do I get ObjectId with a JSON feed? (it's blank)

查看:163
本文介绍了MongoDB PHP:如何使用JSON feed获取ObjectId? (它是空白的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过GridFS存储文件,并保存id:

I'm storing a file through GridFS and saving the id like so:

$file_id = $gridfs->storeUpload('texture');
$item = array(
    'name'=>$_POST['name'],
    'description'=>$_POST['description'],
    'price'=>$_POST['price'],
    'categories'=>$category_array,
    'tags'=>$tag_array,
    'file'=>$file_id
 );
 $collection->insert($item);

并通过终端并执行find()file返回:
ObjectId(4cbe9afe460890b774110000 )

and through terminal and doing find() "file" returns: ObjectId("4cbe9afe460890b774110000")

如果我这样做创建一个JSON feed,所以我可以得到信息回我的应用程序文件是空白...为什么这是? p>

If i do this to create a JSON feed so i can get info back for my application "file" is blank... why is this?:

foreach($cursor as $item){
            $return[$i] = array(
                'name'=>$item['name'],
                'description'=>$item['description'],
                'file'=>$item['file']
            );
            $i++;
        }
        echo json_encode($return);

奇怪的是,对我来说,是为什么我可以这样做:

The strange thing, to me, is why can I do:

foreach($cursor as $item){
echo $item['file'];
}

并返还?

哦,这里是饲料返回的:

Oh, and here is what the feed returns:

[{"name":"Tea Stained Paper 1","description":"Grungy paper texture stained with tea and coffee.","file":{}},{"name":"Worn Metal 1","description":"A grooved, worn old metal texture","file":{}}]


推荐答案

,但可能

echo json_encode($return, JSON_FORCE_OBJECT);

是您需要做的。

也可以,你需要将 $ item ['file'] 转换为utf8

It also could be, that you need to convert $item['file'] to utf8

utf8_encode($item['file']);



< 。

before assigning it to the $return array.

这篇关于MongoDB PHP:如何使用JSON feed获取ObjectId? (它是空白的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆