Here is a line of coldfusion code:
<cfif ArrayIsDefined(valarr, 1)>
which causes this error "The key [VALARR] does not exist."
Well, sometimes valarr does exist and sometimes it doesn't. I'm trying to determine which so that I can proceed to the appropriate code. But I can't find any way to ask whether an array exists at all -- only the code above which asks whether a certain key exists within the array.
Can anyone tell me how to programmatically determine whether there is an array or not?
CodePudding user response:
You is trying to find out if element one exists. Arrays are just variables, so you can test with.
<cfif isDefined(valarr)>
You may want to add
<cfif isDefined(valarr) AND isArray(valarr)>
CodePudding user response:
@James A Mohler already posted an answer while I was writing this, so I'm posting it as an alternative.
If you take a look at the cfml documentation at cfdocs.org for arrayIsDefined(), you'll see that the function "Determines whether an array element is defined in a given index".
In other terms, it only checks if the specified array has a populated value at a specific index number position. What it does not do is to verify whether the array has been created at all.
One possibillity to verify existance of an array is to use the structKeyExists() function with the scope name where that array gets created. That's very likely the variables scope (but I can't say for sure, because I'd need to see more code for that). Also, it's good practice to always use the scope for better security and performance of your cfml engine.
Thus, here is a code snippet as an example that should work for you:
<cfif structKeyExists( variables, "valarr" ) and ArrayIsDefined( variables.valarr, 1)>