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

Apr 25, 2023 pm 05:35 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24