Home  >  Article  >  Backend Development  >  How to convert two-dimensional array to one-dimensional array in PHP

How to convert two-dimensional array to one-dimensional array in PHP

藏色散人
藏色散人Original
2020-07-22 10:13:459483browse

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.

How to convert two-dimensional array to one-dimensional array in PHP

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn