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

How to convert php string to two-dimensional array

PHPz
Release: 2023-04-20 15:09:10
Original
664 people have browsed it

In PHP, converting a string into an array is a very common operation, which is often used in some practical applications. This article will introduce how to use PHP to convert a string into a two-dimensional array. I hope it will be helpful.

The first thing to note is that the strings in this article all conform to a certain format. Therefore, in actual applications, the string needs to be processed and formatted according to the actual situation in order to correctly convert it into an array. In addition, the method provided in this article is not the only one, nor is it necessarily the optimal one. Readers can adjust it according to their own needs and actual situation.

1. String format

Before introducing the specific implementation method, let’s first take a look at the format of the string. The following is an example:

str=0_0:value_1_1:value_2_1:value|1_0:value_1_1:value_2_1:value
Copy after login

The above string is composed of some values, and their format is like this:

a1_b1:value1_a2_b2:value2_a3_b3:value3|a1_b1:value1_a2_b2:value2_a3_b3:value3|...
Copy after login

where a1, a2 , a3 represents the key name of the array, b1, b2, b3 represents the key value of the array, value1 , value2, value3 represents the value to be converted to an array. Each set of values ​​is separated by |, and each value is separated by :.

2. Implementation method

Based on the above string format, we can use the following method to convert it into a two-dimensional array.

  1. explode() function implementation

First, we can use the explode() function to split the string into a one-dimensional array. The following is a code example:

<?php
$str = &#39;0_0:value_1_1:value_2_1:value|1_0:value_1_1:value_2_1:value&#39;;
$arr1 = explode(&#39;|&#39;, $str); // 以 | 分割字符串为一维数组
print_r($arr1);
?>
Copy after login

Run the above code, the output result is:

Array
(
    [0] => 0_0:value_1_1:value_2_1:value
    [1] => 1_0:value_1_1:value_2_1:value
)
Copy after login

As you can see, the string is successfully divided into a one-dimensional array, each element is a A string of length a1_b1:value1_a2_b2:value2_a3_b3:value3.

  1. Loop operation implementation

Next, we can use a loop to parse each string in the one-dimensional array into a two-dimensional array. The following is a code example:

<?php
$str = &#39;0_0:value_1_1:value_2_1:value|1_0:value_1_1:value_2_1:value&#39;;
$arr1 = explode(&#39;|&#39;, $str);  // 以 | 分割字符串为一维数组
$arr2 = array();  // 定义空二维数组
foreach($arr1 as $val) {  // 遍历一维数组
    $tmp_arr = explode(&#39;_&#39;, $val);  // 将字符串分割为键名和键值
    $arr2[$tmp_arr[0]][$tmp_arr[1]] = $tmp_arr[2];  // 将值存入二维数组
    $tmp_arr = explode(&#39;:&#39;, $tmp_arr[3]);  // 将要转换的值分割为一维数组
    foreach($tmp_arr as $k => $v) {
        $tmp_arr[$k] = explode('_', $v);
        $arr2[$tmp_arr[$k][0]][$tmp_arr[$k][1]] = $tmp_arr[$k][2];
    }  // 将一维数组转换为二维数组
}
print_r($arr2);  // 输出结果
?>
Copy after login

Run the above code, the output is:

Array
(
    [0_0] => Array
        (
            [value] => 1
        )

    [1_1] => Array
        (
            [value] => 1
        )

    [2_1] => Array
        (
            [value] => 1
        )

    [1_0] => Array
        (
            [value] => 1
        )

    [2_0] => Array
        (
            [value] => 1
        )

    [1_1] => Array
        (
            [value] => 2
        )

    [2_1] => Array
        (
            [value] => 2
        )

)
Copy after login

As you can see, the string is successfully converted into a two-dimensional array, and each element is a key. Value pairs, where the key name and key value correspond to a1 and b1 in the string respectively.

3. Summary

This article introduces two methods to convert strings into two-dimensional arrays. Among them, the first method uses the explode() function to split the string into a one-dimensional array, and then uses a loop to convert the one-dimensional array into a two-dimensional array; the second method directly uses a loop to parse the string is a two-dimensional array. Readers can choose different methods according to the actual situation in order to better complete the corresponding tasks.

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