Using the PHP Array Module
Article: Effectively using PHP Array module
Overview
The PHP Array module is a versatile tool designed to help you manage and manipulate JSON data within your workflows. This guide provides a detailed explanation of the available actions in the module, along with examples to help you understand their application.
Available Actions
-
Get Value by Index
Get value of a specified index of an array -
Get Array Count
Get count of total number of items in array -
Array Search
Searches an array for a given value and returns the key -
Convert List to Array
Converts a list to an array -
Convert Array to List
Converts an array to a list -
Insert Value at Index
Inserts a value at a specified index in an array -
New Line to Array
Converts a new line separated list to an array -
Extract JSON Data
Extracts JSON data to individual items -
Itemize Array
Converts an array to individual items -
Create Line Items
Creates a list of line items from multiple fields
Action Details with Examples
1. Get Value by Index
This action allows you to get the value of a specified index of an array.
For example, if you have an array of fruits ["apple", "banana", "cherry"]
, you can use this action to get the value at index 1, which is ‘banana’. For more advanced use cases, this works with nested arrays as well. For example, if you have an array of fruits and each fruit has a color, you can get the color of the fruit by specifying the fruit index, eg. your array is - [{"apple": "red"}, {"banana": "yellow"}, {"cherry": "red"}]
, you can get the color of banana by specifying the index as banana, and you will get the response as yellow.
2. Get Array Count
This action allows you to get the count of the total number of items in an array.
For example, if you have an array of fruits ["apple", "banana", "cherry"]
, you can use this action to get the count of the total number of fruits, which is 3.
3. Array Search
This action allows you to search an array for a given value and returns the key of first occurrence.
For example, if you have an array of fruits ["apple", "banana", "cherry"]
, you can use this action to search for the value ‘banana’ and it will return the key 1. A more advanced use case is when you have a nested array of fruits and each fruit has a color, you can search for the color ‘red’ and it will return the key of the first occurrence of the color ‘red’. eg. your array is - [{"apple": "red"}, {"banana": "yellow"}, {"kiwi": "green"}, {"cherry": "red"}]
, you can search for the color ‘red’ and it will return the key of the first occurrence of the color ‘red’. If you search for the color ‘green’, it will return the key of the first occurrence of the color ‘green’, which is 2.
4. Convert List to Array
This action allows you to convert a list to an array. A list is a string of values separated by a delimiter, eg. a comma separated list of fruits - “apple, banana, cherry
”. You can use this action to convert this list to an array of fruits ["apple", "banana", "cherry"]
.
Note: This action will not work with nested lists.
5. Convert Array to List
This action allows you to convert an array to a list. An array is a collection of values, eg. an array of fruits ["apple", "banana", "cherry"]
. You can use this action to convert this array to a comma separated list of fruits - “apple, banana, cherry
”.
Note: This action will not work with nested arrays.
6. Insert Value at Index
This action allows you to insert a value at a specified index in an array.
For example, if you have an array of fruits ["apple", "banana", "cherry"]
, you can use this action to insert a new fruit ‘kiwi’ at index 1, and the new array will be ["apple", "kiwi", "banana", "cherry"]
.
This action also works with nested arrays. For example, if you have an array of fruits and each fruit has a color, you can insert a new fruit with color at a specified index. eg. your array is - [{"apple": "red"}, {"banana": "yellow"}, {"cherry": "red"}]
, you can insert a new fruit ‘kiwi’ with color ‘green’ at index 1, and the new array will be [{"apple": "red"}, {"kiwi": "green"}, {"banana": "yellow"}, {"cherry": "red"}]
.
7. New Line to Array
This action allows you to convert a new line separated list to an array. A new line separated list is a string of values separated by a new line character, eg. a new line separated list of fruits -
apple
banana
cherry
You can use this action to convert this new line separated list to an array of fruits, and you will get the response as [{"list_item":"apple"},{"list_item":"banana"},{"list_item":"cherry"}]
.
8. Extract JSON Data
This action allows you to extract JSON data to individual items. If you have a JSON object with multiple key-value pairs, you can use this action to extract each key-value pair as an individual item. For example, if you have a JSON object - {"name": "John", "age": 30, "city": "New York"}
, you can use this action to extract each key-value pair as an individual item, and you will get the response as -
name - John
age - 30
city - New York
Now, you can map this data in the next action steps in your workflow for further processing.
9. Itemize Array
This action allows you to convert an array to itemized array of individual items. Then, the generated itemized array can be used in the next action steps in your workflow in Iterator module. For example, if you have an array of fruits ["apple", "banana", "cherry"]
, you can use this action to convert this array to an itemized array of individual items, and you will get the response as - [{"item":"apple"},{"item":"banana"},{"item":"cherry"}]
.
10. Create Line Items
This action allows you to create an array of line items from multiple fields. If you have multiple fields with values, you can use this action to create an array of line items with each field value as an individual item. For example, if you have line item fields - name: John, age: 30, city: New York, you can use this action to create an array of line items and return the response as - {"item_1":{"name":"John","age":"30","city":"New York","fm_item_index":1}}
.
These are the available actions in the PHP Array module and how you can effectively use them to manage your JSON data in your workflow. You can combine these actions with other modules in FlowMattic to create powerful workflows for your business needs.
Tips for Effective Use
- Always validate your JSON data structure before processing.
- Use the Extract JSON Data action when working with nested JSON objects.
- Combine multiple actions for complex workflows, such as converting a list to an array and then inserting a value at a specific index.
Additional Context
The PHP Array module is particularly useful for automating data transformations and ensuring consistency in your workflows. By leveraging these actions, you can streamline data processing and reduce manual intervention.
For further assistance, refer to the official documentation or reach out to support.