解决方案OP的具体问题
修订,以提供更完整的解决方案
<!--- First of all, will need to go 1 to ArrayLen() to find the main item id. Doesn't matter what order you go here, but main items should be inserted before children so may as well start at the bottom --->
<cfloop index="i" from="1" to="#ArrayLen(session.mycart)#">
<!--- Is this the main item? --->
<cfif session.mycart[i].id EQ form.itemID>
<!--- It's found the item, is it a main item? We've found the item so entering here to break the loop --->
<cfif session.mycart[i].mainitem>
<!--- Go from ArrayLen() to 1, as if you go 1 to ArrayLen() when deleting you confuse Coldfusion --->
<cfloop index="j" from="#ArrayLen(session.mycart)#" to="1" step="-1">
<cfif session.mycart[j].id EQ form.itemID OR session.mycart[j].mainitemid EQ form.itemID>
<cfset ArrayDeleteAt(session.mycart,j)>
</cfif>
</cfloop>
</cfif>
<!--- This loop is done, so break out --->
<cfbreak>
</cfif>
</cfloop>
在你的帖子,你的国家,你是从索引1索引ArrayLen循环(.. )。如果你正在从数组中删除项目,Coldfusion有点直接,并没有真正注意,所以当你删除5元素数组的元素2时,数组变成4个元素(因为你删除了一个)和索引3的元素现在是索引2,因此它是错过的。
解决方法是从最后开始并向后工作。只要你一次删除最多1条记录,那么这是完全有效的,因为它会继续减少它正在检查的索引,直到你下降到1,这将是第一个元素。
这样你可以通过元素5,4,3,2删除元素2,然后它将检查索引1,它现在仍然是与启动循环时相同的项目,因此不会跳过经验丰富。
如何处理这个
我误解了问题,或者在写这一点,得到了编辑的一些Blurb的,但下面适用所以在这里留下它仍然
你有没有考虑将主要商品的特价商品作为子商品,然后它将整个商品封装在一起,并且删除父商品将删除该商品。我有一个类似的问题,其中项目具有与它们相关的选项,并允许观察层次结构,我决定创建一个与参考数组结合的树。正如一个粗略的例子来说明的原则,来看看这个
<cfscript>
// Create a main item structure
stcMainItem = {
id = CreateUUID(),
somekey = somevalue,
someotherkey = someothervalue,
cost = 123
};
// Create some child item structures, special offers for ex
stcSpecialOfferItem1 = {
id = CreateUUID(),
parent = stcMainItem.id,
cost = 45
};
stcSpecialOfferItem2 = {
id = CreateUUID(),
parent = stcMainItem.id,
cost = 45
};
// Each is added to a reference array
arrItemReference = [];
arrItemRefernce.add(stcMainItem);
arrItemRefernce.add(stcSpecialOfferItem1);
arrItemRefernce.add(stcSpecialOfferItem2);
// Now you decide to delete the main item and direct parents
strIDToDelete = stcMainItem.id;
for (i=ArrayLen(arrItemReference);i>=1;i--) {
if (
arrItemReference[i].id == strIDToDelete
||
arrItemReference[i].parent == strIDToDelete
) {
ArrayDeleteAt(arrItemReference,i);
}
}
</cfscript>
在我实际的代码中,我用的方法创建一个Item.cfc的方式做到了这一点,以应对上述和级联起来删除孙辈等,但原则是健全的。基本上你有一些方法可以让项目作为平面数组和父母,孩子和兄弟姐妹的层次结构进行公开。
信息化点
顺便说一句,你不停地交换“阵列”和“结构”当两者不同,如PHP语言,其中阵列用于参考,这两个方面略有不同。数组包含数值索引从1到n的值,而结构是一个保存与任意键有关的值的对象。不同的原因是它们不构建相同的基本Java对象,因此一些在一个方法上工作的方法不能在另一个上工作。
啊!你刚刚救了我的理智!谢谢你1你的评论说,你从上到下,但实际上从下到上是正确的,再次感谢你! – 2012-02-22 23:40:42
在那里,澄清了一点,底部= 1和顶部= ArrayLen(),没有歧义 – 2012-02-22 23:44:17
真是一个很好的解释!谢谢!你的代码很棒! – 2012-02-22 23:59:01