dongyinglan8707 2014-06-10 02:23
浏览 13
已采纳

如何在ColdFusion中创建数组

Can anyone tell me how to convert this line of PHP to ColdFusion?

$dev_name = "xxx";
$cert_name = "yyy";
$url = "https://xxxx.com/";
$headers = array("X-BONANZLE-API-DEV-NAME: " . $dev_name 
                  , "X-BONANZLE-API-CERT-NAME: " . $cert_name);

Which is an array. I have tried this but it fails.

<cfset dev_name="xxx">
<cfset cert_name ="yyy">
<cfset headers = {X-BONANZLE-API-DEV-NAME:"#dev_name#"
                    , X-BONANZLE-API-CERT-NAME:"#cert_name#"}>

<cfdump var="#headers#">

</cfdump>
  • 写回答

1条回答 默认 最新

  • dougeqiang1619 2014-06-10 03:58
    关注

    You need to quote the key names also same as you did in the PHP version.

    The issue that you are hitting is that your key names contain a "minus" character. To workaround this issue you need to quote your keyname.

    Also in your sample CFML code you will end up creating a struct, it looks to me what you want is an array of structs.

    So something like this should get you what you want:

    <cfset headers = [ {"X-BONANZLE-API-DEV-NAME":"#dev_name#"}
                       , {"X-BONANZLE-API-CERT-NAME":"#cert_name#"} ]>
    

    or if you just want an array of strings:

    <cfset headers = [ "X-BONANZLE-API-DEV-NAME:" & dev_name
                       ,"X-BONANZLE-API-CERT-NAME:" & cert_name ]>
    

    Note: the square brackets which indicate that you want an array and the curly braces indicate you want a struct. It's very similar to JSON notation.

    If you are on an old version of coldfusion you would need to do something like this:

    For a array of structs:

    <cfset headers = arrayNew(1)>
    <cfset headers[1] = structNew()>
    <cfset headers[1]["X-BONANZLE-API-DEV-NAME"] = dev_name>
    <cfset headers[2] = structNew()>
    <cfset headers[2]["X-BONANZLE-API-CERT-NAME"] = cert_name>
    

    OR

    For a array of strings:

    <cfset headers = arrayNew(1)>
    <cfset headers[1] = "X-BONANZLE-API-DEV-NAME:" & dev_name>
    <cfset headers[2] = "X-BONANZLE-API-CERT-NAME:" & cert_name>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败