Creating array from variables
Creating array from variables: compact()
If you have variables such as:
and you need to create an array where the keys correspond to the variable names (βnameβ and βlastnameβ) and the values correspond to the variable values, you can use the compact()
function. This function is particularly useful when you have a method that requires variables to be passed in a compact format to another method. For instance, in controllers, you may use certain variables and then pass them as input values in an array to a template renderer.
After calling the compact()
function with the above examples, you will obtain:
Here an example using the compact()
function:
You can use the compact()
function by passing the names of variables as strings with multiple parameters. Alternatively, you can call compact()
with an array of variable names as strings, which produces equivalent behavior: