Convert one-dimensional array to two-dimensional array php

WBOY
Release: 2023-05-05 22:08:07
Original
2365 people have browsed it

In PHP development, we often need to convert arrays into two-dimensional arrays and one-dimensional arrays. This article will discuss how to convert one-dimensional arrays into two-dimensional arrays.

Basic concepts of one-dimensional arrays and two-dimensional arrays

In PHP, an array is a special variable that can store multiple values. One-dimensional arrays are the simplest array type, in which each element contains only one value. A two-dimensional array is an ordered data collection that contains multiple one-dimensional arrays.

Methods to convert one-dimensional arrays to two-dimensional arrays

In PHP, there are many ways to convert one-dimensional arrays to two-dimensional arrays. Below we introduce some commonly used methods.

Method 1: Use a for loop

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

<?php
$arr = ['a', 'b', 'c', 'd', 'e', 'f'];
$rows = 2;
$cols = 3;

$index = 0;
for ($i = 0; $i < $rows; $i++) {
    for ($j = 0; $j < $cols; $j++) {
        if (isset($arr[$index])) {
            $result[$i][$j] = $arr[$index];
            $index++;
        } else {
            break;
        }
    }
}
print_r($result);
?>
Copy after login

In the code, $arr is the one that needs to be converted. Dimensional array, $rows and $cols represent the number of rows and columns of the two-dimensional array respectively. In the loop, we assign the elements in the one-dimensional array to the two-dimensional array by judging the array subscript. The final output is the converted two-dimensional array.

Method 2: Use array_chunk function

PHP built-in function array_chunk can split a one-dimensional array into multiple array blocks, and then return a new array composed of these blocks. We can use this function to convert a one-dimensional array into a two-dimensional array. The sample code is as follows:

<?php
$arr = ['a', 'b', 'c', 'd', 'e', 'f'];
$cols = 3;

$result = array_chunk($arr, $cols);
print_r($result);
?>
Copy after login

In the code, $arr is the one-dimensional array that needs to be converted, and $cols is the length of each two-dimensional array. Use the array_chunk function to convert a one-dimensional array into a two-dimensional array and output the converted result.

Method 3: Use foreach loop

Another way to convert a one-dimensional array into a two-dimensional array is to use PHP's foreach loop. An example is as follows:

<?php
$arr = ['a', 'b', 'c', 'd', 'e', 'f'];
$rows = 2;
$cols = 3;

$index = 0;
foreach (range(1, $rows) as $i) {
    foreach (range(1, $cols) as $j) {
        if (isset($arr[$index])) {
            $result[$i][$j] = $arr[$index];
            $index++;
        } else {
            break;
        }
    }
}
print_r($result);
?>
Copy after login

In the code, we create an array through the range function to represent the number of rows and columns. Then, we assign the value of the one-dimensional array to the two-dimensional array by judging the subscript in the foreach loop. The final output is the converted two-dimensional array.

Method 4: Use array_chunk and array_pad functions

Another method of using array_chunk and array_pad functions to convert a one-dimensional array into a two-dimensional array is as follows:

<?php
$arr = ['a', 'b', 'c', 'd', 'e', 'f'];
$cols = 3;

$result = array_chunk($arr, $cols);
$last = count($result[count($result) - 1]);
if ($last < $cols) {
    $result[count($result) - 1] = array_pad($result[count($result) - 1], $cols, "");
}
print_r($result);
?>
Copy after login

In the code, We first use the array_chunk function to split the one-dimensional array into multiple array blocks, and then use the array_pad function to fill the last array block with null values. This achieves the conversion of a one-dimensional array into a two-dimensional array.

Summary

This article introduces four methods of converting one-dimensional arrays into two-dimensional arrays. In actual development, we can choose the appropriate method to use according to specific needs. No matter which method, we can achieve array conversion through loops, built-in functions, etc.

The above is the detailed content of Convert one-dimensional array to two-dimensional array 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