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

How to convert string to integer array in php

PHPz
Release: 2023-04-26 11:13:44
Original
1413 people have browsed it

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";
Copy after login
Copy after login
Copy after login

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]; //转换成整型后存入数组
}
Copy after login

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";
Copy after login

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]; //转换成整型后存入数组
}
Copy after login

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";
Copy after login
Copy after login
Copy after login

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

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

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";
Copy after login
Copy after login
Copy after login

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');
Copy after login

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template