Tech Tip: Stringify Array into elements using JSON Commands in v14+
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: April 29, 2015
Here is a completely native (without even calling PHP) way of splitting a Long Integer array into a string value. This new method uses the JSON commands that were added in v14:
// Method: UTIL_Split_Array // Parameters // $1 - pointer to the array // $2 - [OPTIONAL] split character // Return // $0 - string of elemets in the array separated by the split character // ---------------------------------------------------------------------- C_POINTER($1;$arr) C_TEXT($0;$2;$split;$result) If (Count parameters>0) If (Type($1->)=LongInt array) $arr:=$1 $result:=JSON Stringify array($arr->;*) If (Count parameters>=2) $split:=$2 Else $split:="" End if $result:=Replace string($result;"[\n\t";"") // start of json object $result:=Replace string($result;"\n]";"") // end of json object $result:=Replace string($result;",\n\t";$split) // split each element $0:=$result End if End if |
If this method is saved in your dayabase as UTIL_Split_Array it can then be used like this:
ARRAY LONGINT($arr;3) $arr{1}:=1 $arr{2}:=2 $arr{3}:=3 $test:=UTIL_Split_array (->$arr;",") |
You can also use this method like this:
ARRAY LONGINT($arr;3) $arr{1}:=1 $arr{2}:=2 $arr{3}:=3 $test:=UTIL_Split_array (->$arr) |
Previous discussions on this topic included an approach using PHP: Stringify arrays using php implode