Creating an easier to read array/structure hybrid

Here's something I came across today that I had never realized or seen before. I created a two dimensional array and filled it with data.

<cfset myArray = ArrayNew(2)>

<cfset myArray[1][1] = "Charles">
<cfset myArray[1][2] = "President">
<cfset myArray[2][1] = "Vicki">
<cfset myArray[2][2] = "VP">
<cfset myArray[3][1] = "Bob">
<cfset myArray[3][2] = "Controller">
<cfset myArray[4][1] = "David">
<cfset myArray[4][2] = "Programmer">

Pretty much standard array practice, right? Well, I started thinking that sometimes its hard to remember which dimension of the array stores what values. So I started thinking about a more logical format I could use and came up with the following:

<cfset myArray = ArrayNew(1)>

<cfset myArray[1]["name"] = "Charles">
<cfset myArray[1]["title"] = "President">
<cfset myArray[2]["name"] = "Vicki">
<cfset myArray[2]["title"] = "VP">
<cfset myArray[3]["name"] = "Bob">
<cfset myArray[3]["title"] = "Controller">
<cfset myArray[4]["name"] = "David">
<cfset myArray[4]["title"] = "Programmer">

Now each value in the array is holding a structure with two values, name and title. I personally find this to be better and allows for easier readability.

To display all values, all I have to do is:

   <cfloop from="1" to="arrayLen(myArray)" index="i">
      Name: #myArray[i].name#<br>
      Title: #myArray[i].title#<br>

For me, its a little easier to read.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.