Reputation: 699
I have the following piece of PHP code that stores values in an Array but I'm rewriting the application in ColdFusion and don't know what the syntax for doing the same function in ColdFusion is.
$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" => "" ));
Can someone please help?
Upvotes: 1
Views: 618
Reputation: 14333
Another option is to declare each option separately. You also don't need the variables.
prefix. This syntax is probably most commonly used when using the attributecollection=""
option for a tag such as <cfmail>
but will work in any scenario.
<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#" />
Upvotes: 0
Reputation: 14784
What you have there in PHP, looks like what's called a 'structure' or 'object' in ColdFusion.
Try this code, which converts your PHP to a CFML syntax:
<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#" />
It makes use of the {}
declaration, which creates a structure in ColdFusion. You can do it this way just with curly braces (which is called an implicit structure) or using the structNew()
function. The implicit version is the newer and more preferred method.
Please also note that you will need to convert your variables. In PHP your variables are decaled $withTheDollarSign
. In ColdFusion, variables are created using the <cfset />
tag.
These are the same:
PHP
<?php $hello = 'world'; ?>
ColdFusion:
<cfset variables.hello = 'world' />
You could also just write it like:
<cfset hello = 'world' />
However, I like to make good practice of always scoping my variables. The variables scope is the default scope for variables, but it's still good practice to explicitly state this to avoid naming collisions.
Hope this helps. Mikey.
PS - As a bonus point, arrays are created in a very similar fashion, except instead of {}
you would use []
. This is a great article on how to create stucture's and arrays in ColdFusion.
http://www.bennadel.com/blog/740-Learning-ColdFusion-8-Implicit-Struct-And-Array-Creation.htm
Upvotes: 8
Reputation: 9311
This is not an array. It's a map (key-value-pairs). PHP doesn't make a difference between those two constructs (except for "numeric" and "associative" arrays), but ColdFusion (being based on Java) does. In ColdFusion the equivalent would be a struct:
<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>
Upvotes: 4