VBScript Array Push With Flattening Option
Since this is #1 ranked result from Google for VBScript array push, I think we need a little more content.
First of all, the only function that actually works properly "out of the box" (VBScript compiles, indexing properly) is the first post by Travis G.
I'm guilty of blindly copying and pasting (don't worry guys I'm citing the authors / URL for my purposes :)) and ran into a problem. I really did want to build a multidimensional array with this. I appreciate the extra logic that flattens passed in arrays to maintain one container; however, I'd like to control it. In my usage I just removed that section of the code. A flag would be helpful. This is just a slightly modified version of Travis G's original solution.
Code:
Function Push(ByRef mArray, ByVal mValue, blnDoFlatten)
Dim mValEl
If IsArray(mArray) Then
If IsArray(mValue) Then
If blnDoFlatten Then
For Each mValEl In mValue
Redim Preserve mArray(UBound(mArray) + 1)
mArray(UBound(mArray)) = mValEl
Next
Else
Redim Preserve mArray(UBound(mArray) + 1)
mArray(UBound(mArray)) = mValue
End If
End If
Else
If IsArray(mValue) Then
mArray = mValue
Else
mArray = Array(mValue)
End If
End If
Push = UBound(mArray)
End Function