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

PHPz
Release: 2023-04-27 10:14:08
Original
1922 people have browsed it

In PHP, an associative array is usually composed of key-value pairs and can also be multi-dimensional. But in some cases, we need to convert a three-dimensional array into a two-dimensional array to facilitate data processing and display. This article will introduce how to convert a three-dimensional array into a two-dimensional array.

1. Why do we need to convert a three-dimensional array into a two-dimensional array

In some cases, we will obtain a three-dimensional array from the database or other data sources, but when actually processing the data , we only need to use a two-dimensional array. For example, if we have a three-dimensional array containing city and attraction information for all provinces in China, and we want to display this information on a web page, then we need to convert this three-dimensional array into a two-dimensional array to facilitate data processing and display. .

2. How to convert a three-dimensional array to a two-dimensional array

There are many ways to convert a three-dimensional array to a two-dimensional array. This article will introduce two different methods.

1. Use foreach loop

The foreach loop is a very common method of traversing an array. All array elements can be obtained through the foreach loop. For three-dimensional arrays, we can use nested foreach loops to convert them into two-dimensional arrays. The code is as follows:

$sourceArray = array(
    array(
        array("a"=>1,"b"=>2),
        array("a"=>3,"b"=>4),
        array("a"=>5,"b"=>6)
    ),
    array(
        array("a"=>7,"b"=>8),
        array("a"=>9,"b"=>10),
        array("a"=>11,"b"=>12)
    ),
    array(
        array("a"=>13,"b"=>14),
        array("a"=>15,"b"=>16),
        array("a"=>17,"b"=>18)
    )
);

$targetArray = array();
foreach($sourceArray as $province){
    foreach($province as $city){
        $targetArray[] = array_values($city);
    }
}
Copy after login

In the above example, $sourceArray is a three-dimensional array containing three provinces, each province contains multiple cities, and each city contains multiple attractions.

We first use an empty array $targetArray as the target array. For each province, we use the first foreach loop to traverse each city. For each city, we use a second foreach loop to traverse each attraction and add it to $targetArray. Since each city and attraction is an associative array, we convert it into a two-dimensional array through the array_values() function.

In the end, $targetArray becomes a two-dimensional array containing all city and attraction information.

2. Use the array_map function

If you don’t like to use a foreach loop, then we can use the array_map function to convert a three-dimensional array into a two-dimensional array. The code is as follows:

function flattenArray($element){
    return array_values($element);
}

$sourceArray = array(
    array(
        array("a"=>1,"b"=>2),
        array("a"=>3,"b"=>4),
        array("a"=>5,"b"=>6)
    ),
    array(
        array("a"=>7,"b"=>8),
        array("a"=>9,"b"=>10),
        array("a"=>11,"b"=>12)
    ),
    array(
        array("a"=>13,"b"=>14),
        array("a"=>15,"b"=>16),
        array("a"=>17,"b"=>18)
    )
);

$targetArray = array();
foreach($sourceArray as $province){
    $targetArray = array_merge($targetArray,array_map("flattenArray",$province));
}
Copy after login

In this example, we define a function called flattenArray, which is used to convert each element into a two-dimensional array. We then apply this function to each city using the array_map function, converting it into a 2D array and storing it in the $targetArray array.

Finally, $targetArray becomes a two-dimensional array containing all city and attraction information.

3. Summary

This article introduces two methods of converting a three-dimensional array into a two-dimensional array. They are using the foreach loop and the array_map function. Although the code for these two methods is different, their purpose is the same. When we need to process large amounts of data, we can use one of these methods to solve this problem.

The above is the detailed content of How to convert three-dimensional to two-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