Home  >  Article  >  Backend Development  >  How to convert string to array type in php

How to convert string to array type in php

PHPz
PHPzOriginal
2023-04-17 14:12:47428browse

PHP provides a variety of methods to convert strings to array types. This article will list several of them and introduce them in detail.

  1. Split the string into an array based on the delimiter

The explode function is provided in PHP to split the string into an array by specifying the delimiter. For example, the following code separates the string "apple, banana, pear" into an array by commas:

$string = "apple,banana,pear";
$array = explode(",", $string);
print_r($array);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => pear
)
  1. Split the string according to the character length Into an array

Another way to split a string into an array is to use the str_split function. This function will split the string into arrays according to the specified length. For example, the following code splits the string "hello world" into an array according to the length of each character:

$string = "hello world";
$array = str_split($string);
print_r($array);

The output result is:

Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => w
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)
  1. Convert the string to an array , and retain the key name

In addition to the above two methods, you can also use the str_split and array_combine functions to convert the string into an associative array while retaining the key name. For example, the following code converts the string "apple, banana, pear" into an associative array:

$string = "apple,banana,pear";
$array = str_split($string, strlen($string)/3);
$array = array_combine(range(0, count($array)-1), $array);
print_r($array);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => pear
)
  1. Convert the string into a multidimensional array

Finally, we can also use the preg_split function to split the string into multi-dimensional arrays according to regular expressions. For example, the following code splits the string "apple|1,banana|2,pear|3" into a multi-dimensional array according to the regular expressions "/,/u" and "/|/u":

$string = "apple|1,banana|2,pear|3";
$array = preg_split("/,/u", $string);
foreach ($array as &$value) {
    $value = preg_split("/|/u", $value);
}
print_r($array);

The output result is:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => 1
        )

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

    [2] => Array
        (
            [0] => pear
            [1] => 3
        )

)

Summary

The above are several methods for converting strings to array types in PHP. We can choose different methods according to actual needs. Using these methods makes it easier to operate on strings.

The above is the detailed content of How to convert string to array type in 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