Tech Tip: Finding Unique Data in an Array
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: December 12, 2014
Data can contain many duplicate values. There may be the need to only see one instance of a unique data. Below is a method that takes in an array of data and populates another array and populates it with only a single instance of a unique data.
Below is the Unique_Elem() that takes in two string arrays, the first containing an array of data and the second which will contain the result. The method takes advantage of the PHP array_unique command which takes an array and creates a single string of data with a labeled format. The PHP preg_replace command cleans up these labels and extra characters. Then it gets split into an array with the PHP explode command.
C_POINTER($1->;$2->) ARRAY TEXT($arrIn;0) ARRAY TEXT($arrOut;0) C_TEXT($pattern;$resStr;$resClean) C_BOOLEAN($err) COPY ARRAY($1->;$arrIn) $pattern:="[^{|}$|\"\\d*\":|\"]" $err:=PHP Execute("";"array_unique";$resStr;->$arrIn) $err:=PHP Execute("";"preg_replace";$resClean;$pattern;"";->$resStr) $err:=PHP Execute("";"explode";$arrOut;",";->$resClean) COPY ARRAY($arrOut;$2->) |
Below is an example of using the method:
ARRAY TEXT($arr;0) ARRAY TEXT($arrRes;0) APPEND TO ARRAY($arr;"12") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"c") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"b") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"a") APPEND TO ARRAY($arr;"1") APPEND TO ARRAY($arr;"b") APPEND TO ARRAY($arr;"b") APPEND TO ARRAY($arr;"c") APPEND TO ARRAY($arr;"c") Unique_Elem (->$arr;->$arrRes) |
Executing the code above $arrRes results in the following array containing no duplicate values: