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

藏色散人
Release: 2023-03-02 20:48:01
Original
9406 people have browsed it

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"

arrayRequired. 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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!