Home  >  Article  >  Backend Development  >  Convert string to two-digit array php

Convert string to two-digit array php

WBOY
WBOYOriginal
2023-05-06 12:35:07783browse

In PHP programming, converting a string into a two-dimensional array is a common operation, but it is also relatively troublesome. This article will introduce how to use PHP language to convert a string into a two-dimensional array.

1. Convert string to one-dimensional array

First, we need to convert the input string into a one-dimensional array according to certain rules. Suppose we have the following string:

$str = "1,2,3;4,5,6;7,8,9";

This string consists of 3 characters It consists of substrings separated by commas. Each substring consists of three numbers separated by commas. We need to convert this string into a one-dimensional array. The simple way is to use PHP's explode() function to split the string into multiple substrings according to semicolons, and then use loops and the explode() function to split each substring into multiple numbers according to commas, and finally form a one-dimensional array.

The specific code implementation is as follows:

$str = "1,2,3;4,5,6;7,8,9";
$arr = array();
$lines = explode(";", $str);
foreach ($lines as $line) {
    $arr = array_merge($arr, explode(",", $line));
}

This code first splits the input string into multiple substrings according to semicolons, and then uses the loop and explode() function to split each substring. Divided into multiple numbers and merged into a one-dimensional array through the array_merge() function.

2. Convert one-dimensional array to two-dimensional array

Next, we need to convert the obtained one-dimensional array into a two-dimensional array according to certain rules. Suppose we need to convert the one-dimensional array obtained above into a 3x3 two-dimensional array. The specific code implementation is as follows:

$arr = array_chunk($arr, 3);

This code uses PHP's array_chunk() function to convert the one-dimensional array into a 3x3 two-dimensional array. The 3 elements are divided into multiple sub-arrays, and finally form a 3x3 two-dimensional array.

3. Complete code implementation

Combining the above two steps, the complete code implementation is as follows:

$str = "1,2,3;4,5,6;7,8,9";
$arr = array();
$lines = explode(";", $str);
foreach ($lines as $line) {
    $arr = array_merge($arr, explode(",", $line));
}
$arr = array_chunk($arr, 3);
print_r($arr);

This code first divides the input string into The number is split into multiple substrings, and then each substring is split into multiple numbers using a loop and the explode() function, and merged into a one-dimensional array through the array_merge() function. Finally, use the array_chunk() function to divide the one-dimensional array into multiple sub-arrays for every 3 elements to form a 3x3 two-dimensional array, and use the print_r() function to output.

4. Summary

In PHP programming, converting a string into a two-dimensional array is a common operation, but it is also relatively troublesome. Through the above code example, you can learn how to use PHP language to convert a string into a two-dimensional array. At the same time, this also reminds us that we need to pay attention to code writing specifications and efficiency when programming to avoid unnecessary troubles.

The above is the detailed content of Convert string to two-digit array php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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