1

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?

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

4 Comments

Having a look at the docs might help too: help.adobe.com/en_US/ColdFusion/10.0/Developing/…
now why he was answered, evn he did not tried... @Adam note it down
@user3454903 you don't see the difference between what you asked on your question, and what this person asked? They've asked about a single statement. You were asking about dozens of lines of code. Suggest you read this: stackoverflow.com/help/on-topic, specifically point "3".
well even i can split things up, but i know you will still do the same,
4

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>

2 Comments

Note: on CF8 and up you can use {} rather than structNew()
@MattBusche But it's quite debateable if that really improves the readability. ;) And you can always learn to write shorter code later, when you understood what you are doing.
0

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#" />  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.