How to convert a two-dimensional array into a one-dimensional array in php

PHPz
Release: 2023-04-20 14:27:17
Original
708 people have browsed it

PHP is a very popular programming language, especially in the world of web development. For PHP programmers, working with arrays is a very common task. In many cases, we need to convert a two-dimensional array into a one-dimensional array. This article explains how to achieve this in PHP.

What are two-dimensional arrays and one-dimensional arrays?

In PHP, an array can be thought of as a variable type used to store multiple values. Each of these values ​​is called an element, and each element is identified by a unique index. There are two array types in PHP: one-dimensional arrays and two-dimensional arrays.

A one-dimensional array is a simple list consisting of an index and a value, where each value has a unique index. We can use iterators to traverse a one-dimensional array.

A two-dimensional array is a list composed of multiple lists. They consist of a main index and a sub-index. We need to use nested iterators to traverse a two-dimensional array.

How to convert a two-dimensional array into a one-dimensional array?

In PHP, we can easily use the built-in array_column function to convert a two-dimensional array into a one-dimensional array. The array_column function allows us to take a column from a multidimensional array and return a new one-dimensional array.

The syntax of array_column is as follows:

array_column(array $array, mixed $column_key, mixed $index_key = null): array
Copy after login

Among them, the $array parameter is the specified array, $column_key is the key name or position of the value to be returned, and $index_key is used as the new array key. Key name or location. If the $index_key parameter is not provided, a numeric index is used.

The following is an example of converting a two-dimensional array into a one-dimensional array:

$array = array(
    array('id' => 1, 'name' => 'Tom', 'age' => 30),
    array('id' => 2, 'name' => 'Jerry', 'age' => 25),
    array('id' => 3, 'name' => 'Mike', 'age' => 35)
);

$names = array_column($array, 'name');
print_r($names);
Copy after login

This will output the following results:

Array
(
    [0] => Tom
    [1] => Jerry
    [2] => Mike
)
Copy after login

In this example, we use the array_column function to convert The 'name' value of each element in the two-dimensional array $array is extracted to generate a new one-dimensional array $names.

It should be noted that if you want to use a numeric index as the new array key, use a number in the $index_key parameter, as follows:

$ids = array_column($array, 'id', 0);
print_r($ids);
Copy after login

This will output the following results:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Copy after login

In this example, we set the key of the new array to the first element of the original 2D array.

Conclusion

Manipulating arrays is a very common task in PHP development. Converting a two-dimensional array to a one-dimensional array is one operation in this task. In PHP, we can easily achieve this using the array_column function. This function allows us to extract a column from a two-dimensional array and generate a new one-dimensional array.

The above is the detailed content of How to convert a two-dimensional array into a one-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template