Lära känna
To achieve this goal, we can create a custom function that will extract the required variables from both scenarios of the array data. This custom function can utilize the PHP function array_column() to capture the values associated with a given key, then assign these values to corresponding PHP variables. The elements inside of the two scenarios can have a different arrangement and number of elements; as long as the function is set up to loop through all of the possible elements it should be able to extract the right values.
To create this custom function, I recommend the following steps:
1. Define a function such as "extractVariables($array, $arrayVariable)", where $array is the original array data returned from the external server and $arrayVariable is the name of the variable that will correspond to the array.
2. If the data obtained from the external server is not an array, then an error message should be returned.
3. Utilize a foreach loop that looks through the $array data to loop through each element.
4. For each element, use array_column() to extract the values associated with a given key. This will grab the values for “time”, “duration”, and “amount”, which can then be assigned to respective variables.
5. Outside of the loop, assign each extracted value to its corresponding array variable ($time, $duration, and $amount).
6. Lastly, return the newly created array variables.
By following these steps, we can create a custom function to loop through the array data obtained from the external server and extract the appropriate values, then assign them to the corresponding variables.
Best regards,
Giáp Văn Hưng