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

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

PHPz
Release: 2023-04-03 21:00:02
Original
1370 people have browsed it

When programming in PHP, it often involves the need to convert a two-dimensional array into a one-dimensional array. So, how to implement this operation?

First of all, we need to clarify what a two-dimensional array and a one-dimensional array are. In PHP, a one-dimensional array is a comma-separated list of values, such as:

$arr1 = array("apple", "banana", "cherry");
Copy after login

, while a two-dimensional array is an array containing multiple one-dimensional arrays, such as:

$arr2 = array(
  array("apple", 2),
  array("banana", 5),
  array("cherry", 8)
);
Copy after login

Next, let’s talk about how to convert a two-dimensional array into a one-dimensional array. In fact, this operation is very simple, just use PHP's array_column function. The function of this function is to extract a specified column from a two-dimensional array and return a one-dimensional array. The syntax of this function is as follows:

array_column(array, column_key);
Copy after login

Among them, array represents the two-dimensional array of data to be retrieved, and column_key represents the key name of the column to be retrieved.

Let’s look at an example. Suppose we have a two-dimensional array, each one-dimensional array contains the two keys "fruit" and "quantity", and we want to convert it into a one-dimensional array containing only the column "fruit", you can do this :

$arr2 = array(
  array("fruit" => "apple", "quantity" => 2),
  array("fruit" => "banana", "quantity" => 5),
  array("fruit" => "cherry", "quantity" => 8)
);

$fruits = array_column($arr2, "fruit");

print_r($fruits);
Copy after login

The output result of the above code is:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)
Copy after login

As you can see, by calling the array_column function, we successfully extracted the "fruit" column from the two-dimensional array and got A one-dimensional array containing only fruit names.

In addition to passing the key name, we can also pass the integer parameter as the key name to indicate the position of the column to be extracted in the two-dimensional array. For example:

$arr3 = array(
  array("apple", 2),
  array("banana", 5),
  array("cherry", 8)
);

$fruits = array_column($arr3, 0);

print_r($fruits);
Copy after login

The output of the above code is the same as the previous example.

It should be noted that if the column to be extracted does not exist in the two-dimensional array, or the key name or positional parameter is passed incorrectly, a warning will be generated.

The above introduces how to convert a two-dimensional array into a one-dimensional array. I hope it will be helpful to your PHP programming.

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!

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