Tech Tip: Utility Method to Find the Number of Times a Value Appears in an Array
PRODUCT: 4D | VERSION: 14.x | PLATFORM: Mac & Win
Published On: May 5, 2016
Below is a method that takes in an Array and returns, in an Object, the number of times each value appears in the array.
// Util_GetArrayValueCount // // Desription: // Gets the Count of each unique values // from an array into an object // // Parameters: // $1: Pointer to an Array // Results: // $0: Object containing Array Values as Object Attribute, and // Counts of the Values as Object Value // ---------------------------------------------------- C_POINTER($1;$inArr_ptr) C_OBJECT($0) C_TEXT($result_t) If (Count parameters=1) $inArr_ptr:=$1 PHP Execute("";"array_count_values";$result_t;$inArr_ptr) $0:=JSON Parse($result_t) End if |
Sample of Method:
ARRAY LONGINT($al;0) APPEND TO ARRAY($al;1) APPEND TO ARRAY($al;1) APPEND TO ARRAY($al;1) APPEND TO ARRAY($al;2) APPEND TO ARRAY($al;2) APPEND TO ARRAY($al;2) APPEND TO ARRAY($al;2) APPEND TO ARRAY($al;2) APPEND TO ARRAY($al;2) APPEND TO ARRAY($al;2) APPEND TO ARRAY($al;3) APPEND TO ARRAY($al;3) APPEND TO ARRAY($al;4) APPEND TO ARRAY($al;4) C_OBJECT($o) $o:=Util_GetArrayValueCount (->$al) TRACE |
Result: