zip()

Bundles elements at the same position from multiple arrays into tuples and returns them as an array.

Syntax

zip(ARRAY1, ARRAY2[, ...])

Parameters

ARRAY1, ARRAY2[, ...]
The arrays or values to bundle. Specify two or more, separated by commas (,).

Description

The zip() function pairs elements at the same position from multiple arrays into tuples and returns an array of those tuples. If arrays have different lengths, the shorter arrays are padded with null once exhausted. Scalar values are used only in the first tuple, then padded with null. Returns null if all arrays are empty.

Error codes

N/A

Usage examples

  1. Bundle two arrays element by element.

    json "{}"
    | eval keys = array("a", "b", "c"), vals = array(1, 2, 3)
    | eval result = zip(keys, vals)
    | # result: [["a",1],["b",2],["c",3]]
    
  2. Use arrays of different lengths. The shorter array is padded with null once exhausted.

    json "{}"
    | eval arr1 = array("x", "y"), arr2 = array(10, 20, 30)
    | eval result = zip(arr1, arr2)
    | # result: [["x",10],["y",null]]
    
  3. Use an array and a scalar value together. The scalar is used only in the first tuple.

    json "{}"
    | eval arr = array("a", "b", "c")
    | eval result = zip(arr, "fixed")
    | # result: [["a","fixed"],["b",null],["c",null]]
    
  4. Returns null when all arguments are empty arrays.

    json "{}" | eval result = zip(array(), array())
    | # result: null
    

Compatibility

zip() has been available since before Sonar 4.0.