Can't output simple array?

[亡魂溺海] 提交于 2019-12-13 19:12:53

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/46432318/cant-output-simple-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!