无法输出简单数组? [英] Can't output simple array?
问题描述
我一直在试图弄清为什么我的数组不输出。
我在数组上做了一个var dump,它可以很好地转储,但是当我尝试输出它,它返回致命错误。另外,我发现也许我可能必须遍历数组才能访问它,所以我也尝试了一下,并通过执行var dump设法遍历了数组,但是当我输出它时,它破坏了页面。 / p>
这是我尝试过尝试将近3个小时的代码,尝试各种事情。
ColdFusion:
< cfset defaultDirectory = C:\uploads\ />
< cfdirectory
directory =#defaultDirectory#
action = list
name = myList
>
<!---获取目录名称数组--->
< cfquery dbtype = query name = fileNames>
从myList
中选择名称
< / cfquery>
<!-创建数组--->
< cfset myArray = arraynew(1)>
<!---使用目录查询数据名称填充数组--->
< cfloop query = fileNames>
< cfset myArray [CurrentRow] [1] =#defaultDirectory#&姓名& \>
< / cfloop
到目前为止,一切似乎都进展顺利。我转储了数组变量,并输出了我想要的。因此,我尝试了此操作,并返回了致命错误。
< cfoutput>
< cfif directoryExists(#myArray [1]#)>
它存在。
< cfelse>
不存在。
< / cfif>
< cfoutput>
这是要转储的变量的屏幕截图。
然后我想也许是我必须遍历数组才能访问它?
所以我尝试了这个。
< cfloop index = i从= 1到=#arrayLen(myArray)#>
< cfdump var =#myArray [i]#
< / cfloop>
这设法转储了数组中的所有内容,但是当我尝试输出时,它返回一个致命错误我不确定为什么。我查阅了有关如何转储阵列的教程,但不确定自己做错了什么吗?任何建议,将不胜感激。
以下是请求的myArray结构的屏幕截图:
我做了
< cfvar dump =#myArray#>
您有一系列结构。因此,您不能简单地将数组值输出为字符串。
您需要引用数组索引以及结构的键以提取值。
像这样的伪代码: arrayName [arrayIndex] [structureKey]
。看起来您可以在初始代码示例中使用它。此处: < cfset myArray [CurrentRow] [1] =#defaultDirectory#&姓名& \>
这让我感到困惑,因为您的结构键恰好是数字 1
。因此,它看起来像是索引或类似内容。
我创建了一个要点,向您展示了一个有效的示例-
因此您的代码应如下所示:
< cfif directoryExists(#myArray [1] [1]#)>
它存在。
< cfelse>
不存在。
< / cfif>
或者如果将其包装成这样的循环:
< cfloop index = i从= 1到=#arrayLen(myArray)#>
< cfif directoryExists(#myArray [i] [1]#)>
它存在。
< cfelse>
不存在。
< / cfif>
< / cfloop>
I've been trying to figure out why my array won't output.
I did a var dump on the array and it dumps it just fine but when I try to output it, it returns fatal error. In addition, I figured that maybe I had to probably loop over the array in order to access it, so I tried that too and managed to loop over the array by doing a var dump but when I output it, it breaks the page.
Here is my code that I attempted i've been trying for almost 3 hours now trying various things.
ColdFusion:
<cfset defaultDirectory = "C:\uploads\" />
<cfdirectory
directory="#defaultDirectory#"
action="list"
name="myList"
>
<!---Get Array of Directory Names --->
<cfquery dbtype="query" name="fileNames">
SELECT NAME
FROM myList
</cfquery>
<!---Create array --->
<cfset myArray=arraynew(1)>
<!---Populate array with directory query data "name" --->
<cfloop query="fileNames">
<cfset myArray[CurrentRow][1]=#defaultDirectory# & NAME & "\">
</cfloop
Up to this point, everything seemed to be going good. I dumped out the array variable and it outputted what I wanted. So I tried this and it returned a fatal error.
<cfoutput>
<cfif directoryExists("#myArray[1]#")>
it exists.
<cfelse>
Doesn't exists.
</cfif>
<cfoutput>
Here is the screenshot of the variable being dumped.
What I then figured was maybe I have to loop over the array in order to access it?
so I tried this.
<cfloop index="i" from="1" to="#arrayLen(myArray)#">
<cfdump var="#myArray[i]#"
</cfloop>
this managed to dump out all things from the array but when i try to output it, it returns a fatal error I'm not sure why. I looked up tutorials on how to dump the array and I'm not sure what I'm doing wrong? Any suggestions would be appreciated.
Here is a screenshot of the structure of myArray that was requested:
I did
<cfvar dump="#myArray#">
You have an array of structures. So you cannot simply output the array value as a string; that is why you are getting an error.
You need to reference the array index as well as the structure's key to extract the value. Something like this pseudo-code: arrayName[arrayIndex][structureKey]
. It looks like you have it working in the initial code example. Here:
<cfset myArray[CurrentRow][1]=#defaultDirectory# & NAME & "\">
It was confusing to me because your structure key happens to be the number 1
. So it looked like an index or something.
I created a gist to show you a working example - TryCF Gist. Here is the code I wrote:
<cfscript>
structA = {1="C:\uploads\101 San Fernando"};
structB = {1="C:\uploads\121 Tasman"};
structC = {1="C:\uploads\360 Residences"};
structD = {1="C:\uploads\481 On Mathilda"};
myArray = [];
ArrayAppend(myArray,structA);
ArrayAppend(myArray,structB);
ArrayAppend(myArray,structC);
ArrayAppend(myArray,structD);
writeDump(myArray);
//writeOutput(myArray[1][1]);
for (i=1;i LTE ArrayLen(myArray);i=i+1) {
writeOutput('<p>' & myArray[i][1] & '</p>');
}
</cfscript>
The output for that code is:
So your code should look like this:
<cfif directoryExists("#myArray[1][1]#")>
it exists.
<cfelse>
Doesn't exists.
</cfif>
Or if you wrap that in a loop like this:
<cfloop index="i" from="1" to="#arrayLen(myArray)#">
<cfif directoryExists("#myArray[i][1]#")>
it exists.
<cfelse>
Doesn't exists.
</cfif>
</cfloop>
这篇关于无法输出简单数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!