The join_hide function, which expands on the join function, can be used to join the elements of an array into a string.
Syntax
string join_hide(string $separator, string[] $arr)
Concatenates the elements of $arr using the specified $separator between each element, while ignoring null
strings or whitespaces.
Parameters
$separator: The separator that will be inserted between the elements of $arr in the generated string.
$arr: The array the elements of which will be joined. It is worth mentioning that only arrays of strings and arrays of numbers are supported by the function. Arrays of arrays or objects won’t generate a result and will trigger an error.
Example
In the dataset below, two arrays are included. The first array is an array of strings, that also contains an empty string, while the second array is an array of numbers.
{
"FirstandLastNames": [ //Array of strings
"Mads",
"Jakob",
"", //Empty string
"Madsen"
],
"Numbers": [ //Array of numbers
1,
2,
3,
4
]
}
Usually, the separator parameter is hardcoded (typed by the user) within single apostrophes (fx. ‘separator‘), but of course, it is possible to fetch this from a different key from the sample data or from an already defined key in the transformation using the $ operator. In this case, the most commonly used separators, i.e. an empty space (‘ ‘) and a comma (‘, ‘), will be used. Additionally, an example with a line break separator (
) is also included, and, lastly, the difference between the join_hide and the join function is demonstrated in the last example.`"\n"`
{
JH1: join_hide(', ', FirstandLastnames),
JH2: join_hide(' ', Numbers),
JH3: join_hide(`"\n"`, FirstandLastnames),
J: join(', ', FirstandLastnames)
}
In the 3rd example above (JH3), it is worth noticing that instead of using the single apostrophes for the separator parameter, backticks
(` `
), also known as grave accents, are used. This is normally the case when the line break separator is defined in such functions.
Overall, as seen in the output data below, using the join_hide function it is possible to create a string that consists of the elements (string or number values) of an array, separated by a defined separator. Regarding the third example (JH3), while in the output, the values are separated with “\n”, in the generated document they will actually be separated with a line break.
Lastly, as seen in the last example (J), when using the join function, the empty string that is included in the “FirstandLastnames” array, is not omitted. This is basically the main difference between join
and join_hide
.
{
"JH1": "Mads, Jakob, Madsen", //Array values joined in a string, separated by a comma and a space. Empty string is ignored
"JH2": "1 2 3 4", //Separated by empty spaces
"JH3": "Mads\nJakob\nMadsen", //In this case, \n indicates a line break. The line break can only be seen in the generated document
"J": "Mads, Jakob, , Madsen" //The join function includes the empty string in the joined string.
}
It is worth mentioning that if null
is defined as a separator, the values will be joined without any spaces or characters between them.
Notes
The join_hide function will also work with arrays that are defined within the function, as long as they consist of strings or numbers. The example below is using the same dataset that was presented earlier in this article.
{
Example: join_hide(' | ', [FirstandLastnames[0], 'Andreas', Numbers[2], '', FirstandLastnames[3]])
}
As it can be noticed, the array parameter isn’t pointing to an existing array in the sample data, but it is constructed in the actual parameter and incorporates a hardcoded entry, an empty string, and 3 values from the two arrays that are in the sample data. The above transformation, leads to the following result:
{
"Example": "Mads | Andreas | 3 | Madsen"
}
As expected, the hardcoded empty string was omitted and not included in the joined string.