arrayset
Array Operation
[arrayset name=...&dim=....][/arrayset]
So far you have been using variables quite a lot. You have put numbers into [math ] variables, and you have put text into [text] variables. But you have only done this one at a time: you've put one number into a variable, or one string of text. So one variable was holding one piece of information. An array is a variable that can hold more than one piece of information at a time.
An array allows the programmer to store more than one value in a variable, whilst retaining a single reference. If we think of a variable as a single slot in memory (or a box) that can contain data of a certain type - number, character, etc. - then an array is the equivalent of a box divided into partitions, each containing a piece of data.
The array context will allow the WebDNA programmer to create an array data object with up to five dimensions.
Parameters
-Name - The variable name you specify to represent the array object.
-Dim - Comma delimited series of numbers representing the array dimension sizes, i.e. '3,4,5', which would represent a 3x4x5 dimensioned array:
Here is a simple example of a single dimension array:
...the tag '[DOW(7)]' would resolve to 'Sat'
You can replace array values with new ones:
...the tag '[DOW(7)]' would resolve to 'joker'
and add new ones, only if the original arrayset context specifies a size allowing to add new values:
...the tag '[DOW(8)]' would resolve to 'Next week'
Here an example of a three-dimensional array:
This creates an array, called 'myarray', that will persist for the duration of the template. Note that the 'index=value' pairs within the [arrayset] tags are optional when creating a new array. Array indices can be assigned at any time.
When the 'dim' parameter is supplied with the arrayset context, it implies that this will be a new array object. Subsequent calls to arrayset, using the same variable name (and without using the ‘dim’ parameter), imply new index assignments to an existing array object.
So after we have created the 'myarray' object using the line shown above, we can assign index value at a later time using:
Once you have created an array, you can access the 'elements' of the array using the following methods:
You can use a 'global tag' reference to retrieve a single array element. Here is the syntax for the global method:
would show
value1
value2
value4
value3
value6
value7
Another way would be to use the [arrayget] context to retrieve several index values. Here we will discuss using the [arrayget] contex
[arrayget] takes just one parameter; 'name', in which you place an existing array variable name. The (a,b,c,d,e) patterns evaluate to the corresponding index values. These patterns can be intermingled with other text, as shown.
Index 1,1,1 = value1
Index 1,1,2 = value2
Index 1,2,1 = value3
Index 1,2,2 = value4
Index 2,1,1 =
Index 2,1,2 = value6
Index 2,2,1 =
Index 2,2,2 = value7
[numdims] Tag
You can use the [numdims] tag to retrieve the number of dimensions for an array. For example:
Results:
array_1 contains 3 dimensions.
You can also use the 'array name' global tag to retrieve the number of dimension in a named array object. Here is the code:
Results:
array_1 contains 3 dimensions.
[dimsize_] Tag
The [dimsize_] tag is used to retrive the size of a given array dimension.
Example:
Results:
array_1 has 3 dimensions:
Dimension 1 has 3 indexes.
Dimension 2 has 4 indexes.
Dimension 3 has 5 indexes.
You can also access the dimension sizes using the global 'array name' tag, as follows:
Results:
array_1 has 3 dimensions:
Dimension 1 has 3 indexes.
Dimension 2 has 4 indexes.
Dimension 3 has 5 indexes.
You can't code PHP without knowing about array's because array's are just about the basis of PHP. WebDNA does not need active use of array's like other languages because of its flat-file database system: with WebDNA, array's can easily and advantageously be replaced by databases to serve as virtual unlimited permanent array's.
So far you have been using variables quite a lot. You have put numbers into [math ] variables, and you have put text into [text] variables. But you have only done this one at a time: you've put one number into a variable, or one string of text. So one variable was holding one piece of information. An array is a variable that can hold more than one piece of information at a time.
An array allows the programmer to store more than one value in a variable, whilst retaining a single reference. If we think of a variable as a single slot in memory (or a box) that can contain data of a certain type - number, character, etc. - then an array is the equivalent of a box divided into partitions, each containing a piece of data.
The array context will allow the WebDNA programmer to create an array data object with up to five dimensions.
A multi-dimensional array is an array of arrays. If we think of a one-dimensional array as a row of slots in a box, then a two dimensional array is a grid of slots, and a three dimensional array is a cube of slots. Beyond three dimensions it becomes difficult to conceptualize, but theoretically at least, arrays can have any dimension. WebDNA allows up to 5 dimensions
Parameters
-Name - The variable name you specify to represent the array object.
-Dim - Comma delimited series of numbers representing the array dimension sizes, i.e. '3,4,5', which would represent a 3x4x5 dimensioned array:
Here is a simple example of a single dimension array:
[arrayset name=DOW&dim=7](1)=Sun&(2)=Mon&(3)=Tue&(4)=Wed&(5)=Thu&(6)=Fri&(7)=Sat[/arrayset]
...the tag '[DOW(7)]' would resolve to 'Sat'
You can replace array values with new ones:
[arrayset name=DOW](7)=joker[/arrayset]
...the tag '[DOW(7)]' would resolve to 'joker'
and add new ones, only if the original arrayset context specifies a size allowing to add new values:
[arrayset name=DOW&dim=8](1)=Sun&(2)=Mon&(3)=Tue&(4)=Wed&(5)=Thu&(6)=Fri&(7)=Sat[/arrayset]
[arrayset name=DOW](8)=Next week[/arrayset]
...the tag '[DOW(8)]' would resolve to 'Next week'
Here an example of a three-dimensional array:
[arrayset name=myarray&dim=2,2,2]
(1,1,1)=value1&(1,1,2)=value2&(1,2,1)=value3&(1,2,2)=value4
[/arrayset]
This creates an array, called 'myarray', that will persist for the duration of the template. Note that the 'index=value' pairs within the [arrayset] tags are optional when creating a new array. Array indices can be assigned at any time.
When the 'dim' parameter is supplied with the arrayset context, it implies that this will be a new array object. Subsequent calls to arrayset, using the same variable name (and without using the ‘dim’ parameter), imply new index assignments to an existing array object.
So after we have created the 'myarray' object using the line shown above, we can assign index value at a later time using:
[arrayset name=myarray](2,1,2)=value6&(2,2,2)=value7[/arrayset]
Once you have created an array, you can access the 'elements' of the array using the following methods:
You can use a 'global tag' reference to retrieve a single array element. Here is the syntax for the global method:
[<arrayname>(<index>)]
[myarray(1,1,1)]
[myarray(1,1,2)]
[myarray(1,2,2)]
[myarray(1,2,1)]
[myarray(2,1,1)]
[myarray(2,1,2)]
[myarray(2,2,2)]
[myarray(2,2,1)]
would show
value1
value2
value4
value3
value6
value7
Another way would be to use the [arrayget] context to retrieve several index values. Here we will discuss using the [arrayget] contex
[arrayget name=myarray]
[loop start=1&end=2]
[loop start=1&end=2]
[loop start=1&end=2]
Index [::::index],[::index],[index] = ([::::index],[::index],[index])<br>
[/loop]
[/loop]
[/loop]
[/arrayget]
[arrayget] takes just one parameter; 'name', in which you place an existing array variable name. The (a,b,c,d,e) patterns evaluate to the corresponding index values. These patterns can be intermingled with other text, as shown.
Index 1,1,1 = value1
Index 1,1,2 = value2
Index 1,2,1 = value3
Index 1,2,2 = value4
Index 2,1,1 =
Index 2,1,2 = value6
Index 2,2,1 =
Index 2,2,2 = value7
[numdims] Tag
You can use the [numdims] tag to retrieve the number of dimensions for an array. For example:
[arrayset name=array_1&dim=3,3,10][/arrayset]
array_1 contains [arrayget name=array_1][numdims][/arrayget] dimensions.
Results:
array_1 contains 3 dimensions.
You can also use the 'array name' global tag to retrieve the number of dimension in a named array object. Here is the code:
[arrayset name=array_1&dim=3,3,10][/arrayset]
array_1 contains [array_1(numdims)] dimensions.
Results:
array_1 contains 3 dimensions.
[dimsize_] Tag
The [dimsize_] tag is used to retrive the size of a given array dimension.
Example:
[arrayset name=array_1&dim=3,4,5][/arrayset]
[arrayget name=array_1]
array_1 has [numdims] dimensions:
[loop start=1&end=[numdims]]
Dimension [index] has [interpret][dimsize_[index]][/interpret] indexes.
[/loop]
[/arrayget]
Results:
array_1 has 3 dimensions:
Dimension 1 has 3 indexes.
Dimension 2 has 4 indexes.
Dimension 3 has 5 indexes.
You can also access the dimension sizes using the global 'array name' tag, as follows:
[arrayset name=array_1&dim=3,4,5][/arrayset]
array_1 has [array_1(NumDims)] dimensions:
[loop start=1&end=[array_1(NumDims)]]
Dimension [index] has [interpret][array_1(DimSize_[index])][/interpret] indexes.
[/loop]
Results:
array_1 has 3 dimensions:
Dimension 1 has 3 indexes.
Dimension 2 has 4 indexes.
Dimension 3 has 5 indexes.