Home > Article > Backend Development > How to convert numeric string into array in php
In PHP programming, it is a very common requirement to convert a number string into an array, such as reading a column of numbers in a csv file, storing it as an array for subsequent operations, or reading a column of numbers from a database. , also needs to be converted into an array for processing. This article introduces several methods to convert digital strings into arrays.
Method 1: Use str_split function
str_split function can split the string into an array, for example:
$str = "123456"; $arr = str_split($str); print_r($arr);
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
This kind The method is very simple, but it should be noted that if the number string contains a negative sign or a decimal point, this method will not handle it correctly.
Method 2: Use the preg_split function
The preg_split function can split the string into an array according to the specified delimiter, for example:
$str = "1,2,3,4,5,6"; $arr = preg_split('/,/', $str); print_r($arr);
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
This method is more flexible than str_split, and different delimiters can be specified according to the actual situation.
Method 3: Use the explode function
The explode function can also split the string into an array according to the specified delimiter, for example:
$str = "1,2,3,4,5,6"; $arr = explode(',', $str); print_r($arr);
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
Compared with the preg_split function, the explode function is more concise, but its performance is slightly worse than preg_split.
Method 4: Use the str_split and array_map functions
This method combines the str_split and array_map functions to convert the digital string into an array, for example:
$str = "123456"; $arr = array_map('intval', str_split($str)); print_r($arr);
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
This method uses the array_map function to convert a string array into an integer array, while maintaining the splitting capability of the str_split function.
Method 5: Use regular expressions
If you need to parse complex numeric strings, such as "1,2,3 | 4,5,6 | 7,8,9", Regular expressions can be used for parsing, for example:
$str = "1,2,3 | 4,5,6 | 7,8,9"; $arr = preg_split('/[\s|,]+/', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($arr);
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 )
This method uses [\s|,] in regular expressions to match any spaces, vertical bars and comma, and use the PREG_SPLIT_NO_EMPTY flag to ensure that no empty elements are split.
Summary
The above methods can convert digital strings into arrays, each with its own advantages and disadvantages. It is necessary to choose the most suitable method according to the actual situation.
The above is the detailed content of How to convert numeric string into array in php. For more information, please follow other related articles on the PHP Chinese website!