在数组索引解封它变成一个对象 [英] Unsetting index in array turns it to an object

查看:134
本文介绍了在数组索引解封它变成一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个JSON文件我存储在CouchDB中。下面是它的重要组成部分:

So I have a JSON document I'm storing in CouchDB. Here's the important part of it:

"games": {
   "creator": [
       "cf86d68b24c1bbf22702356572027642",
       "cf86d68b24c1bbf22702356572027dd8",
       "cf86d68b24c1bbf22702356572028b77"
   ],
   "solver": {
   }
}

我想从数组中删除一个项目,让我们说指数1:

I'm trying to remove one item from the array, let's say index 1:

error_log("pre unset: " . json_encode($user->games));
unset($user->games->creator[1]);
error_log("post unset: " . json_encode($user->games));

问题是,它使我的数组转换成一个对象,像这样:

The problem is, it keeps converting my array to an object, like so:

pre unset: {"creator":["cf86d68b24c1bbf22702356572027642","cf86d68b24c1bbf22702356572027dd8","cf86d68b24c1bbf22702356572028b77"],"solver":{}}
post unset: {"creator":{"0":"cf86d68b24c1bbf22702356572027642","2":"cf86d68b24c1bbf22702356572028b77"},"solver":{}}

这是怎么回事,以及如何解决这个问题?

What's going on, and how do I fix it?

推荐答案

我固定它是这样的:

unset($user->games->creator[1]);
$user->games->creator = array_values($user->games->creator);

这篇关于在数组索引解封它变成一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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