Link to home
Start Free TrialLog in
Avatar of SiobhanElara
SiobhanElaraFlag for United States of America

asked on

Inserting an array into another array with ColdFusion

I have some code that checks a form for errors and puts any in an array. The results might look something like this:

------------------------------------------------------------------------------------------
Array: Errors
------------------------------------------------------------------------------------------
name        | Please enter your name.
------------------------------------------------------------------------------------------
questions  | Please enter your questions or comments
------------------------------------------------------------------------------------------

I have another array that needs to contain the information from this array, which should look something like this:

------------------------------------------------------------------------------------------
Array: data
------------------------------------------------------------------------------------------
success        | False
------------------------------------------------------------------------------------------
errors           | name        | Please enter your name.
------------------------------------------------------------------------------------------
                     | questions  | Please enter your questions or comments
------------------------------------------------------------------------------------------

I can't for the life of me figure out how to get the first array into the second array. Following is the code I have so far:

<cfset errors = ArrayNew(2)>
<cfset data = ArrayNew(3)>

<cfif Trim(FORM.name) EQ "">
	<cfset errors[1][1] = "name">
	<cfset errors[1][2] = "Please enter your name.">
</cfif>
<cfif Trim(FORM.email) EQ "" OR NOT IsValid("email", FORM.email)>
	<cfset errors[2][1] = "email">
	<cfset errors[2][2] = "Please enter a valid email.">
</cfif>
<cfif Trim(FORM.phone) EQ "">
	<cfset errors[3][1] = "phone">
	<cfset errors[3][2] = "Please enter your phone number.">
</cfif>
<cfif Trim(FORM.questions) EQ "">
	<cfset errors[4][1] = "questions">
	<cfset errors[4][2] = "Please enter your questions or comments.">
</cfif>

<cfif NOT Len(errors)>
	<cfset data[1][1] = "success">
	<cfset data[1][2] = "false">
	<cfset data[2][1] = "errors">
 	<cfset ArrayInsertAt(data[2], 2, errors)>
<cfelse>
	<cfset data[1][1] = "success">
	<cfset data[1][2] = "true">
	<cfset data[2][1] = "message">
	<cfset data[2][2] = "Thank you for your inquiry! We'll be in touch with you soon.">
</cfif>

Open in new window


The ArrayInsertAt obviously doesn't work, it's just the latest in my string of failed attempts. What should I be doing here? Thanks!
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(no points ...)

FWIW - I completely agree with gdemaria.  His approach is much more intuitive.
Avatar of SiobhanElara

ASKER

That is SO much better! Thank you.