Home  >  Article  >  Backend Development  >  php string conversion two-digit array

php string conversion two-digit array

王林
王林Original
2023-05-19 09:27:08524browse

In PHP programming, converting a string into an array is a common operation. In actual development, if you need to convert a string into a two-dimensional array, you need to use some special methods and techniques. In this article, we will discuss how to convert a string into a two-dimensional array using PHP.

1. Use the explode() function

First, we can use the explode() function in PHP to convert the string into a one-dimensional array, and then convert it into a two-dimensional array. The explode() function is a function used in PHP to split a string into an array. It accepts two parameters: the first parameter is the delimiter, and the second parameter is the string to be split. For example, the following code splits a string into an array using "," as the delimiter:

$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)

Next, we can use a loop statement to convert the one-dimensional array into a two-dimensional array. For example, the following code converts a one-dimensional array into a two-dimensional array:

$str = "apple,banana,orange;dog,cat,fish";
$arr1 = explode(";", $str);

foreach ($arr1 as $key => $value) {
    $arr2[$key] = explode(",", $value);
}

print_r($arr2);

The output result is:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => banana
            [2] => orange
        )

    [1] => Array
        (
            [0] => dog
            [1] => cat
            [2] => fish
        )

)

2. Use the preg_split() function

In addition to using explode( ) function, we can also use the preg_split() function in PHP to split the string into an array. Different from the explode() function, the preg_split() function uses regular expressions to split. For example, the following code splits a string into an array using ";" and "," as delimiters:

$str = "apple,banana,orange;dog,cat,fish";
$arr = preg_split("/[,;]/", $str);
print_r($arr);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => dog
    [4] => cat
    [5] => fish
)

Next, we can also use loop statements Convert a one-dimensional array to a two-dimensional array. For example, the following code converts a one-dimensional array into a two-dimensional array:

$str = "apple,banana,orange;dog,cat,fish";
$arr1 = preg_split("/;/", $str);

foreach ($arr1 as $key => $value) {
    $arr2[$key] = preg_split("/,/", $value);
}

print_r($arr2);

The output result is:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => banana
            [2] => orange
        )

    [1] => Array
        (
            [0] => dog
            [1] => cat
            [2] => fish
        )

)

3. Use the json_decode() function

In addition to using strings In addition to the split function, we can also use JSON format to convert the string into a two-dimensional array. The json_decode() function in PHP can convert a JSON-formatted string into a PHP array. For example, the following code uses JSON format to convert a string into a two-dimensional array:

$str = '{"fruits": ["apple", "banana", "orange"], "animals": ["dog", "cat", "fish"]}';
$arr = json_decode($str, true);
print_r($arr);

The output result is:

Array
(
    [fruits] => Array
        (
            [0] => apple
            [1] => banana
            [2] => orange
        )

    [animals] => Array
        (
            [0] => dog
            [1] => cat
            [2] => fish
        )

)

4. Summary

In PHP programming, Converting a string to a two-dimensional array is a basic operation. In addition to using the string splitting function, we can also use JSON format to convert the string into a two-dimensional array. In general, which method to choose depends on the actual situation. If the string format is complex, it is recommended to use JSON format. If the string format is relatively simple, it is recommended to use the string split function. No matter which method you use, pay attention to the correctness of the format and syntax to ensure a successful conversion.

The above is the detailed content of php string conversion two-digit array. 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
Previous article:Set php array to emptyNext article:Set php array to empty