Adobe ColdFusion 8

ArrayIsDefined

View comments | RSS feed

Description

Determines whether an array element is defined.

Returns

True, if the array element is defined (exists); false, otherwise.

Category

Array functions

Function syntax

ArrayIsDefiend(array, elementIndex)

See also

ArrayIsEmpty

History

ColdFusion 8: Added this function.

Parameters

Parameter

Description

array

Name of a one dimensional array, or the array name and indexes into higher-order dimensions of a multidimensional array.

elementIndex

Index of the element in a one dimensional array, or the index of the element in the final dimension of a multidimensional array.

Usage

To test the existence of an element in a multidimensional array, specify all but the last dimension of the array in the first parameter. For example, the following line tests the existence of element MyArray[2][4][1]:

ArrayIsDefined(MyArray[2][4], 1)

Example

<h3>ArrayIsDefined Example</h3>
<!--- Create a sparse new array. --->
<cfset MyArray = ArrayNew(1)>
<!--- Populate an element or two. --->
<cfset MyArray[1] = "Test">
<cfset MyArray[3] = "Other Test">

<cfoutput>
    <!--- Display the contents of the array. --->
    <p>Your array contents are:
    <cfdump var="#MyArray#"></p>
    
    <!--- Check if an existing element is defined. --->
    <p>Does element 3 exist?:&nbsp;
    #ArrayIsDefined(MyArray, 3)#</p>
    
    <!--- Check if a non-existent element is defined. --->
    <p>Does element 2 exist?&nbsp;
    #ArrayIsDefined(MyArray, 2)#
</cfoutput>



Comments


carehart said on Oct 17, 2007 at 5:34 PM :
the "function syntax" section misspells the function as ArrayIsDefiend.
tomj said on Nov 16, 2007 at 10:26 AM :
The function syntax has a typo: ArrayIsDefiend
apatan said on Dec 11, 2007 at 6:20 AM :
This function currently throws an error when the array is NOT initialized.
halL said on Dec 20, 2007 at 2:10 PM :
Apatan is correct, so if it is possible that the array is not yet created, you should surround the ArrayIsDefined test in a <cfif IsDefined("arraynanme")> block.
Also, the ArrayIsDefined function throws an error if you test for an element beyond the length of the array.
To prevent this error, you can surround the test in a test using the ArrayLen function.

The following code shows this usage. You can further test it by commenting out all the code that creates the array and sets any values.

<h3>ArrayIsDefined Example</h3>
<!--- Create a sparse new array. --->
<cfset MyArray = ArrayNew(1)>
<!--- Populate an element or two. --->
<cfset MyArray[1] = "Test">
<!--- <cfset MyArray[3] = "Other Test"> --->

<cfif isdefined("MyArray")>
<cfoutput>
<!--- Display the contents of the array. --->
<p>Your array contents are:
<cfdump var="#MyArray#"></p>

<!--- Check if an existing element is defined. --->
<!--- First make sure the array length is not less than the highest index to test. --->
<cfif (ArrayLen(MyArray) GTE 1) >
<p>Does element 1 exist?:&nbsp;
#ArrayIsDefined(MyArray, 1)#</p>
</cfif>
<cfif (ArrayLen(MyArray) GTE 3) >
<p>Does element 3 exist?:&nbsp;
#ArrayIsDefined(MyArray, 3)#</p>

<!--- Check if a non-existent element is defined. --->
<p>Does element 2 exist?&nbsp;
#ArrayIsDefined(MyArray, 2)#</p>
</cfif>
</cfoutput>
</cfif>
Reto said on Jan 25, 2008 at 2:21 PM :
In the function Syntax the function is called ArrayIsDefiend insted of ArrayIsDefined.
Reto said on Jan 25, 2008 at 2:29 PM :
The function only works if the passed elementIndex is lower than the length of the array. Would be nice if this is written in the docs, would have saved me some minutes.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_a-b_12.html