VBScript array_chunk function

A VBScript equivalent of PHP’s array_chunk


Function array_chunk(mAry,size)

    If not isNumeric(size) Then Exit Function
    If size < 1 then Exit Function

    Dim x,i,c : x = 0 : c = -1
    Dim l : l = uBound(mAry)
    Dim n : n = int(l / size)
    ReDim tmpAry(n)

    For i = 0 to l
        x = i Mod size

        If x >= 1 Then
            If isObject(mAry(i)) Then
                set tmpAry(c)(x) = mAry(i)
            Else
                tmpAry(c)(x) = mAry(i)
            End If
        Else
            c = c +1
            If n <> c Then
                toReDim tmpAry(c),size -1
            Else
                toReDim tmpAry(c),l -i
            End If

            If isObject(mAry(i)) Then
                set tmpAry(c)(0) = mAry(i)
            Else
                tmpAry(c)(0) = mAry(i)
            End If

        End If
    Next

    array_chunk = tmpAry

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the array extension