Home >Backend Development >PHP Problem >How to convert two-dimensional array to one-dimensional array in PHP
PHP method to convert a two-dimensional array into a one-dimensional array: 1. Use the "array_column($array, column_key, index_key);" method to achieve conversion; 2. Use the "array_combine(keys, values)" method to achieve the conversion Convert.
1.array_column($array, column_key,index_key); Returns the value of a single column in the array.
This method benefits from the internal usage of PHP array_column($array,column_key,index_key) version 5.5
Recommended: "PHP Tutorial"
array Required. Specifies the multidimensional array (record set) to be used.
column_key :Required. The column whose value needs to be returned.
Can be an integer index of a column of an index array, or a string key value of a column of an associative array.
This parameter can also be NULL, in which case the entire array will be returned (very useful when used with the index_key parameter to reset the array key).
index_key : Optional. Column used as index/key of the returned array
2. Both arrays are equal in size The arrays are merged into one array. The key (value) of one array becomes the key of the new array.
A new array is created by merging two arrays, and one of the array elements is the key name. The other array element is the key value: 5.4
This method benefits from PHP’s internal usage array_combine(keys,values)
The above is the detailed content of How to convert two-dimensional array to one-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!