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

How to convert string to integer array in php

PHPz
PHPzOriginal
2023-04-26 10:33:371378browse

In PHP, we often need to convert strings into integer arrays for further processing of data. Below we will introduce several methods to convert a string into an array of integers.

Method 1: Use explode() function

explode() function can split a string into an array according to the specified delimiter. By using this function and type conversion, we can convert a comma separated string into an array of integers.

For example, we have the following string:

$str = "1,2,3,4,5";

Then you can use the following code to convert it into an integer array:

$arr = explode(",", $str);
for ($i = 0; $i < count($arr); $i++) {
    $arr[$i] = (int)$arr[$i]; //转换成整型后存入数组
}

Method 2: Use the preg_split() function

Similar to the explode() function, the preg_split() function can also split a string into an array according to a specified pattern. This method is slightly more cumbersome than the explode() function, but can handle more complex strings.

For example, we have the following string:

$str = "1 | 2 | 3 | 4 | 5";

Then you can use the following code to convert it into an integer array:

$arr = preg_split("/[|]+/", $str);
for ($i = 0; $i < count($arr); $i++) {
    $arr[$i] = (int)$arr[$i]; //转换成整型后存入数组
}

Method 3: Use array_map() function

array_map() function is a very useful function that can perform specified processing on each element in the array. We can pass the intval() function as a parameter to the array_map() function, which converts the string to an integer.

For example, we have the following string:

$str = "1,2,3,4,5";

Then you can use the following code to convert it into an integer array:

$arr = array_map('intval', explode(',', $str));

Method 4: Use array_walk() function

Similar to the array_map() function, the array_walk() function also performs specified processing on each element in the array. We can transform each element using array_walk() function. But in this case, we need to define a callback function to perform the conversion, and then use the array_walk() function to call the callback function.

For example, we have the following string:

$str = "1,2,3,4,5";

Then you can use the following code to convert it into an integer array:

function convertToInt(&$value) {
    $value = (int)$value;
}

$arr = explode(',', $str);
array_walk($arr, 'convertToInt');

Conclusion

The above four Either method can convert a string into an array of integers. Among them, the method using the array_map() function is relatively simple, and the method using the preg_split() function is relatively the most flexible. Which method to use depends on the specific situation being dealt with.

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