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

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

PHPz
Release: 2023-04-25 18:03:56
Original
1147 people have browsed it

In PHP development, array operations are very common, and they can effectively help us store and process large amounts of data. We are also relatively proficient in common two-dimensional array operations. However, some developers may get confused when we encounter three-dimensional arrays. This article will introduce how to convert a three-dimensional array into a two-dimensional array.

1. What is a three-dimensional array

In PHP, a three-dimensional array is actually an array with three dimensions. A three-dimensional array can be defined in the following way:

$arr = array(
  array(
    array("apple", 10),
    array("banana", 20)
  ),
  array(
    array("orange", 15),
    array("grape", 25)
  )
);
Copy after login

In the above array, the indexes of the first dimension are 0 and 1, the indexes of the second dimension are 0 and 1, and the indexes of the third dimension are 0 and 1. The values ​​in are the fruit names and corresponding quantities.

Convert two-dimensional array to two-dimensional array

In development, we usually prefer to use two-dimensional array to process data because it is easier to operate and process. So how to convert a three-dimensional array to a two-dimensional array?

We can use double-layer traversal to extract each element of the three-dimensional array and add it to the new two-dimensional array. The specific implementation code is as follows:

function threeDimensionalToTwoDimensional($arr) {
  $result = array();
  foreach ($arr as $value1) {
    foreach ($value1 as $value2) {
      array_push($result, $value2);
    }
  }
  return $result;
}

$arr = array(
  array(
    array("apple", 10),
    array("banana", 20)
  ),
  array(
    array("orange", 15),
    array("grape", 25)
  )
);

print_r(threeDimensionalToTwoDimensional($arr));
Copy after login

Execute the above code and you will get the following output result:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => 10
        )

    [1] => Array
        (
            [0] => banana
            [1] => 20
        )

    [2] => Array
        (
            [0] => orange
            [1] => 15
        )

    [3] => Array
        (
            [0] => grape
            [1] => 25
        )

)
Copy after login

The above output result is the data that has been converted into a two-dimensional array. We can see that there are four elements in the new two-dimensional array, and each element is an array of two values.

3. Summary

Through the above example code, we can see how to convert a three-dimensional array into a two-dimensional array. This is a very useful skill for developers who develop using PHP. In daily development, we are likely to need to operate on three-dimensional arrays from different data sources, and converting them into two-dimensional arrays can make it easier to process the data.

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