从多维数组中删除元素 [英] Remove element from multidimensional array
问题描述
我有以下多维数组:
{"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}}
在此示例中,我要删除
"1":{"cid":"1","uid":"3"}
.我已经尝试了在stackoverflow上找到的所有解决方案,但无法使它们中的任何一个起作用.我主要是php人,所以在这里我可能会错过一些重要的事情吗?
from it. I have tried all solutions that i found on stackoverflow, and could not make any of them work. I am mostly php person, so i might miss something important here?
推荐答案
只需将 delete
与相应的属性一起使用.
Just use delete
with the appropriate property.
var obj = {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}};
delete obj["1"];
注意1周围的"
,将其标记为标识符而不是数组索引!
Note the "
around the 1 to mark it as an identifier and not as an array index!
编辑正如注释中指出的那样,无论您如何寻址 [1]
属性, obj
是一个对象,并且没有数组.我的最后一句话只是为了清楚说明您正在使用对象而不是数组.
EDIT
As pointed out in the comment, obj
is an object and no array no matter how you address the [1]
property. My last note was just to make it clear that you are working with an object and not an array.
根据我的经验,大多数人将整数属性与数组关联,并将字符串属性与对象关联.因此,我认为以指定的方式突出显示该属性可能会更有用.
In my experience most people associate integer properties with arrays and string properties with objects. So I thought it might be more helpful to highlight the property in the way given.
这篇关于从多维数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!