Tech Tip: How to parse a string into multiple array element using PHP function
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: March 27, 2014
Text strings can be parsed into a 4D array through 4D code from this tech tip How to parse a string into multiple array elements. This same functionality can be implemented with the PHP "explode" function.
A method shown below achieves the conversion using the PHP function "explode":
// ----------------------------------------------------------------- // Name: STRING_OF_VALUES_TO_4D_ARRAY // Description: Takes in an Text of values and converts to a 4D // Array Text using PHP "explode" function // // Parameters: // $1 (Text) - Text String that contains data // $2 (Text) - Type of character that is delimited // $3 (Pointer) - Pointer to a 4D Array Text C_LONGINT($i) //Indexing counter C_TEXT($1) //Input of string C_TEXT($input) //Temp variable to remove brackets C_TEXT($2) //Input of the delimeter character C_POINTER($3;$data) //Pointer ARRAY TEXT($4d_array;0) //Output of converted 4D Array Text $input:=$1 $data:=$3 If (Count parameters>=3) $isOK:=PHP Execute("";"explode";$4d_array;$2;$input) For ($i;1;Size of array($4d_array)) APPEND TO ARRAY($data->;$4d_array{$i}) End for End if |
Below is example #1 of using the method using '-':
C_TEXT($input) ARRAY TEXT($output;0) $input:="1-2-3-4-5-6" STRING_OF_VALUES_TO_4D_ARRAY($input;"-";->$output) |
The code above will populate the array $output shown below:
Below is example #2 of using the method using ',' and two values as a set with a semicolon:
C_TEXT($input) ARRAY TEXT($output;0) $input:="A;2,B;4,C;6" STRING_OF_VALUES_TO_4D_ARRAY($input;",";->$output) |
The code above will populate the array $output shown below: