将PHP数组转换为ColdFusion语法

时间:2013-03-12 11:46:02

标签: php arrays coldfusion

我有一段PHP代码,它将值存储在一个Array中,但我在ColdFusion中重写应用程序,并且不知道在ColdFusion中执行相同功能的语法是什么。

$data = array("isReadOnly" => false, "sku" => "ABCDEF", "clientVersion" => 1, "nuc" => $NUC, "nucleusPersonaId" => $personaID, "nucleusPersonaDisplayName" => $dispname, "nucleusPersonaPlatform" => $platform, "locale" => $locale, "method" => "idm", "priorityLevel" => 4, "identification" => array( "EASW-Token" => "" ));

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:8)

你在PHP中有什么,看起来像ColdFusion中的'结构'或'对象'。

尝试使用此代码,将PHP转换为CFML语法:

<cfset variables.data = {

    "isReadOnly" = false, 
    "sku" = "ABCDEF", 
    "clientVersion" = 1, 
    "nuc" = variables.NUC, 
    "nucleusPersonaId" = variables.personaID, 
    "nucleusPersonaDisplayName" = variables.dispname, 
    "nucleusPersonaPlatform" = variables.platform, 
    "locale" = variables.locale, 
    "method" = "idm", 
    "priorityLevel" = 4, 
    "identification" = { "EASW-Token" = "" }

} />

<cfdump var="#variables.data#" />

它使用了{}声明,它在ColdFusion中创建了一个结构。您可以通过花括号(称为隐式结构)或使用structNew()函数以这种方式执行此操作。隐式版本是更新且更受欢迎的方法。

另请注意,您需要转换变量。在PHP中,您的变量被解析为$withTheDollarSign。在ColdFusion中,使用<cfset />标记创建变量。

这些是相同的:

<强> PHP

<?php $hello = 'world'; ?>

<强> ColdFusion的:

<cfset variables.hello = 'world' />

你也可以这样写:

<cfset hello = 'world' />

但是,我喜欢总是确定我的变量范围。变量范围是变量的默认范围,但是明确说明这一点以避免命名冲突仍然是一种好习惯。

希望这会有所帮助。 MIKEY。

PS - 作为奖励点,数组的创建方式非常相似,除了使用{}而不是[]。这篇关于如何在ColdFusion中创建结构和数组的文章很棒。

http://www.bennadel.com/blog/740-Learning-ColdFusion-8-Implicit-Struct-And-Array-Creation.htm

答案 1 :(得分:4)

这不是一个数组。这是一张地图(键值对)。 PHP在这两个结构之间没有区别(“数字”和“关联”数组除外),但ColdFusion(基于Java)确实如此。在ColdFusion中,等价物将是一个结构:

 <cfscript>
     data = structNew();
     data["isReadOnly"] = false;
     data["sku"] = "ABCDEF";

     // You can also nest structs, if need be
     data["identification"] = structNew();
     data["identification"]["EASW-Token"] = "";
 </cfscript>

答案 2 :(得分:0)

另一个选择是单独声明每个选项。您也不需要variables.前缀。对attributecollection=""等标记使用<cfmail>选项时,最常使用此语法,但在任何情况下都可以使用。

<cfset data = {} /> // create a struct
<cfset data.isReadOnly = false />
<cfset data.sku = 'ABCDEF' />
<cfset data.clientVersion = 1 />
<cfset data.nuc = NUC />
<cfset data.nucleusPersonaId = personaID />
<cfset data.nucleusPersonaDisplayName = dispname /> 
<cfset data.nucleusPersonaPlatform = platform />
<cfset data.locale = locale />
<cfset data.method = 'idm' />
<cfset data.priorityLevel = 4 />
<cfset data.identification = { EASW-Token = '' } />

<cfdump var="#data#" />