在 Coldfusion 中显示结构信息 [英] Displaying Struct information in Coldfusion
本文介绍了在 Coldfusion 中显示结构信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示结构键的值,例如:
I want to display the value of a struct key like:
#stReviewDetail['tags']['travelParty']['value']#
可能缺少 tags
、travelParty
或 value
.检查结构层次结构是否可用的最佳方法是什么?比如:
It is possible that tags
, travelParty
or value
is missing. What is the best way to check if the structure hierarchy is available? Something like:
<cfif StructKeyExists(stReviewDetail, 'tags') AND
StructKeyExists(stReviewDetail['tags'], 'travelParty') AND
StructKeyExists(stReviewDetail['tags']['travelParty'], 'value') >
....
</cfif>
或者有更好的方法吗?
推荐答案
多个 StructKeyExists 很丑,写一个函数来简化这个很容易:
Multiple StructKeyExists are ugly, and it's easy to write a function to simplify this:
用法:
<cfif CheckNestedKeys(stReviewDetail,['tags','travelParty','value']) >
#stReviewDetail['tags']['travelParty']['value']#
</cfif>
代码:
<cffunction name="CheckNestedKeys" returntype="Boolean" output=false>
<cfargument name="Struct" type="Struct" required />
<cfargument name="Keys" type="Array" required />
<cfset var CurStruct = Arguments.Struct />
<cfloop index="local.CurKey" array=#Arguments.Keys# >
<cfif StructKeyExists(CurStruct,CurKey)>
<cfset CurStruct = CurStruct[CurKey] />
<cfelse>
<cfreturn false />
</cfif>
</cfloop>
<cfreturn true />
</cffunction>
这篇关于在 Coldfusion 中显示结构信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文