In this write-up, you will be learning the use of array_column() and array_combine() functions in php.
The function array_column() is used to output the values from the input array by considering the single column of the input. You can also learn php with deep understanding by joining w3training school online or offline php training programs.
The function array_combine() is used to create an array of elements in such a way that the certain array values correspond to the array values of the keys.
We will be making it more clear to you in the below-described section. So let’s start with the array_column() function.
ARRAY_COLUMN() FUNCTION
Syntax : array_column($input, $column_key, $index_key);
Three parameters are used in this function which are described below.
- $input- It represents the array of elements from which the value is to be returned in the output. It can also represent a multi-dimensional array.
- $column_key- It represents the mixed value(integer, string, associative array etc.) of the column to be returned.
- $index_key- It is an additional parameter used to index the values in the output array.
Result/ Return Value : On the successful execution of the function, an array of elements corresponding to a single column of the input array will be returned.
Example : An example of array_column() function is shown below.
Output : The following output will be generated after running the above php script. Here, we have used ‘$a’ as the input array. From the input, we have selected ‘last_name’ as a single column key and ‘id’ as the index key. Clearly, the output will show the selected column from the input array as shown in the image below.
ARRAY_COMBINE() FUNCTION
Syntax : array_combine($keys, $values);
Two parameters are used in this function which are described below.
- $keys- It represents an array of keys to which certain values are assigned.
- $values- It represents an array of values that are being used to create an array of elements.
Result/ Return Value : The successful execution of the function will return an array of elements being used in the keys and the values. False will be returned in case the number of keys and respective values are not same.
Errors : If the number of keys used are not equal to the number of respective values, anE_WARNING error is produced on the output screen.
Example : We have shown below an example of array_combine() function.
Output : You will get the following output after running the above php script. Here, we have taken a variable ‘$e’ to which keys ‘a’, ‘b’, ‘c’ and ‘d’ are assigned. Another variable ‘$f’ has been used having values ‘Apple’, ‘Ball’, ‘Cat’ and Dog’. To use the function array_combine() and to store the return value we have used a third variable ‘$g’. Clearly, the number of keys are equal to the number of respective values, so this function will not throw an E_WARNING and the output will look like something as shown in the image below.
We have tried to give you a precise information about the use of array_column() and array_combine() functions in php. Hope you liked this article. For further updates and information, keep visiting our php blogs.