문제

I'm wondering if there is an effective way to put a menu into an array or any other data type. With php I would do something like this:

$menu[1] = "home";
$menu[2] = "news";
$menu[3]["item"] = "products";
$menu[3]["subMenu"][1] = "jackets";
$menu[3]["subMenu"][2] = "T-shirts";
$menu[4] = "contact";

However I have no clue how one would do this in coldfusion. I want to grab this data from the DB and push it into an object, this will allow me to generate the html from the array.

도움이 되었습니까?

해결책

It's actually very similar. This presumes ColdFusion 8 (or higher) for array ([]) and struct ({}) literals:

<cfset menu = [] /> <!--- Create initial array --->

<cfset menu[1] = "home" />
<cfset menu[2] = "news" />
<cfset menu[3] = {} /> <!--- Create structure --->
<cfset menu[3]["item"] = "products" /> <!--- Address structure by key --->
<cfset menu[3]["subMenu"] = [] />
<cfset menu[3]["subMenu"][1] = "jackets" />
<cfset menu[3]["subMenu"][2] = "T-shirts" />
<cfset menu[4] = "contact" />

<cfdump var="#menu#" /> <!--- Output --->

Hope that helps!

다른 팁

To take Ciaran's answer a step farther, you can do it completely with object literals in CF 9:

<cfset menu = ["home",
               "news",
               {"item"="products",
               "subMenu"= ["jackets",
                           "T-shirts"]},
               "contact"]>
<cfdump var="#menu#" /> <!--- Output --->
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top