KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Taking a selection and creating a 4D Array Object of field names and record result
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: February 9, 2018

Here is a method that will take a selection of records to output to a 4D Array Object that contains the field names corresponding to the field values:

// ----------------------------------------------------------------------
// Name: RECORD_SELECTION_2_ARR_OBJ
// Description: Method will take a selection of records of a table and
// generate a 4D Object of field names as a property and record values.
//
//
// Parameters:
// $1 (POINTER) - Table to pointing to the selection
// $2 (POINTER) - Pointer to array object
// ----------------------------------------------------------------------
C_POINTER($1;$tablePtr)
C_POINTER($2;$arrObjPtr)
C_LONGINT($i;$j;$vlTable;$fieldLastNum)
C_POINTER($fieldPtr)
C_OBJECT($obj)

If (Count parameters=2)
  $tablePtr:=$1
  $arrObjPtr:=$2
  $vlTable:=Table($tablePtr)
  $fieldLastNum:=Get last field number($vlTable)
   
  For ($j;1;Records in selection($tablePtr->)
    For ($i;1;$fieldLastNum)
     $fieldPtr:=Field($vlTable;$i)
     
     If (Type($fieldPtr->)=Is object)
      OB SET($obj;Field name($vlTable;$i);OB COPY($fieldPtr->))
     Else
      OB SET($obj;Field name($vlTable;$i);$fieldPtr->)
     End if
    End for
   APPEND TO ARRAY($arrObjPtr->;$obj)
   CLEAR VARIABLE($obj)
   NEXT RECORD($tablePtr->)
  End for
End if


Here is an example of a selection with the sample code using RECORD_SELECTION_2_ARR_OBJ:



ALL RECORDS([Table_1])
ARRAY OBJECT($arrObj;0)

RECORD_SELECTION_2_ARR_OBJ (->[Table_1];->$arrObj)


The result of the formed 4D Array Object: