Introduction To A New Array Type: Key-only Array

By Howard Stone (2010 October 2)

In general, there are two types of array, indexed array and associative array. The array introduced in this article does contain keys only. So far, the best use of key-only array is to represent the dataset in table data type. Let us start from a table in database which contains a dataset as a result of SELECT query.

A normalized table usually has less group relationship between records, that is more to stateless. For this nature, the applications of normalized table tend to deal with a single record once at a time. On contrast, the denormalized table usually has group relationship between records, which may generated from SELECT query that includes GROUP BY clause. This kind of dataset then would be process bulky for sake of relationship between each rows. The major usages include feet data to option group, list box, etc.

The key-only array in Javascript, for instance, for above table would be:

; var arr = new Array(13)
; arr['Language'] = new Array(1)
; arr['Language']['Order'] = 1
; arr['English'] = new Array(1)
; arr['English']['1'] = 0
; arr['Spanish'] = new Array(1)
; arr['Spanish']['2'] = 0
...
; arr['Arabic'] = new Array(1)
; arr['Arabic']['12'] = 0

Actually, this is not so-called new type, it is just a multidimensional associative array, for which some values become keys. Please note, the values of 1 or 0 here are useless. To having values is indeed a compromised setting to enable current design of languages to parse the key-only array without any change. However, the concept of key and value changed since. The container of full information is the keys. The values here thus can be used as flag at programmer's discretion.

Why such? Because it enables to access selected information simply through hierarchical traverse by using keys without complicated parse process. The key-only array does naturally contain the internal relationship between rows, which was generated by original SELECT statement in SQL query. In addition, in comparison with traditional arrays, this would use less codes.

Please note, the key-only array can include or exclude the title of the columns in original table. If any field is null, please turn it into string 'NULL' first. It would be then accessible in key-only array. Please use bracket '' for all keys because the fields in original table may contain whitespace, which would cause problems when key-only array being used in AJAX or other Dynamic HTML.

Let us see how to use this key-only array. When calling it at higher dimension, such as arr['English'] would assign a set of new sub arrays that contain all cells after dimension "English". To get list of the dimension immediately after "English", which is often used for cascade triggering, following codes can be used:

; var immediateList = new array()
; for (key in arr["English"]) immediateList.push(key)

In JSON, it would be:

; var arr =
{"Language":{"Order":1}
,"English":{"1":0}
,"Spanish":{"2":0}
...
,"Arabic":{"12":0}
}

Only at very root, there is array bracket []. All rest are in brackets {}. To access sub arrays started from "English" or "1" after "English" assume there is further sub arrays:

arr["English"]
arr["English"]["1"]

If "1" is something other than numeric, it can be called:

arr["English"].something

To access keys at next dimension after "English":

; var immediateList = new array()
; for (key in arr["English"]) immediateList.push(key)

Summary:

1. Key-only array can use all existing functionalities of indexed array and associative array.
2. Since the information is key itself, there is no need to do any value search. It thus achieves directly random access without traverse.
3. It makes contained relationship information between original rows usable.
4. It uses less code.
5. It can be in JSON format too.

Special notes:

1. Since values are not in use, it can be used to represent last column. However, it is not recommended because it would require special treatment for leave of key tree.
2. Replace null with string 'NULL" at SQL query if the data source is database.
3. Always use bracket to make sure all keys are in string datatype.
4. When calling key-only array, always use array calling method rather than identifier calling method, that is arr["English"]["something"] instead of arr.English.something. This is to avoid possible error because in some languages like JavaScript identifiers can't begin with a numeral.

No comments:

Post a Comment

Labels