Home Backend Development PHP Problem How to convert string to array in php

How to convert string to array in php

Apr 26, 2023 am 10:29 AM

In PHP, string is a very common data type. Usually we need to split the string into a set of characters and store them in an array. for processing and processing in the following code. Here are some methods to convert a PHP string to a PHP array:

Method 1: Use the str_split function

The str_split function can convert a string into an array of single characters. The syntax of this function is as follows:

array str_split ( string $string [, int $split_length = 1 ] )
Copy after login

Among them, the string parameter is the string to be converted, and the split_length parameter is the length of each element in the array to be generated (default is 1). For example:

$str = "Hello world";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);
Copy after login

Output result:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => w
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)

Array
(
    [0] => Hel
    [1] => lo
    [2] => wor
    [3] => ld
)
Copy after login

Method 2: Use str_split and array_map functions combined

If you want to convert a string into a numeric array, you can use the following method :

$str = '123456789';
$arr = array_map('intval', str_split($str));

print_r($arr);
Copy after login

Output result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)
Copy after login

Method 3: Use preg_split function

If you need to split the string into an array according to the specified delimiter or regular expression, You can use the preg_split function. This function returns an array whose elements are substrings divided by strings. The syntax of this function is as follows:

array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
Copy after login

Among them, pattern is the regular expression to be matched, subject is the string to be split, the limit parameter specifies the maximum number of elements to be generated, and flags specifies the regular expression flag. For example:

$str = "apple,banana,orange";
$arr = preg_split("/[\s,]+/", $str);

print_r($arr);
Copy after login

Output result:

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

Method 4: Use the explode function

Similar to preg_split, if you need to split the string into an array according to the specified delimiter , you can use the explode function. The syntax of this function is as follows:

array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
Copy after login

Among them, the delimiter parameter is the specified delimiter, the string parameter is the string to be split, and the limit parameter specifies the maximum number of elements to be generated. For example:

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

print_r($arr);
Copy after login

Output result:

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

The above are several methods of converting PHP strings into PHP arrays. You can choose the appropriate method and apply it flexibly according to your needs.

The above is the detailed content of How to convert string to array in php. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)