KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to Combine 2 or more arrays
PRODUCT: 4D | VERSION: 15.1 | PLATFORM: Mac & Win
Published On: September 21, 2016

The techtip here provides a method using PHP to combine 2 or more array into one: https://kb.4d.com/assetid=77195.

Below is an utility method that will combine the arrays without using PHP.

// Method: UTIL_COMBINE_ARRAY
// Parameters
// $1 - Pointer to array that will contain the result
// ${2} - Pointer to the arrays to combine
// ----------------------------------------------------
ARRAY POINTER($arrP;0)
C_POINTER($1;${2})
C_POINTER($arrResult;0)
$arrResult:=$1

$paramLength:=Count parameters
If ($paramLength>1)
   For ($i;2;$paramLength)
      $arrR:=${$i-1}
      $arrL:=${$i}
      For ($j;1;Size of array($arrL->))
         APPEND TO ARRAY($arrResult->;$arrL->{$j})
      End for
   End for
End if


Example:
ARRAY TEXT($arrResults;0)
ARRAY TEXT($a1;0)
ARRAY TEXT($a2;0)
ARRAY TEXT($a3;0)
ARRAY TEXT($a4;0)
APPEND TO ARRAY($a1;"a")
APPEND TO ARRAY($a1;"b")
APPEND TO ARRAY($a1;"c")
APPEND TO ARRAY($a2;"d")
APPEND TO ARRAY($a2;"e")
APPEND TO ARRAY($a2;"f")
APPEND TO ARRAY($a2;"g")
APPEND TO ARRAY($a3;"h")
APPEND TO ARRAY($a3;"i")
APPEND TO ARRAY($a4;"j")
APPEND TO ARRAY($a4;"k")
APPEND TO ARRAY($a4;"l")
APPEND TO ARRAY($a4;"m")

UTIL_COMBINE_ARRAY (->$arrResults;->$a1;->$a2;->$a3;->$a4)

The results of $arrResults are shown below: