Home >Backend Development >PHP Problem >Convert php string array to int

Convert php string array to int

王林
王林Original
2023-05-05 22:38:06702browse

In PHP, converting a string array to an integer array is a common operation. This article explores how to do this.

First of all, we need to clarify a concept: what is a string array? Simply put, a string array is an array of strings. As shown below:

$arr = array("1", "2", "3", "4");

This is an array containing four strings. Now we want to convert it to an array of integers, how to do it?

A simple way is to use the array_map function that comes with PHP. This function accepts a callback function and an array as parameters, applies the callback function to each array element, and returns a new array.

The sample code is as follows:

$arr = array("1", "2", "3", "4");

$newArr = array_map(function($value) {
    return intval($value);
}, $arr);

print_r($newArr);

In the above code, we set the callback function to intval(), which can convert a string into an integer. So array_map will iterate through each element in the $arr array and pass it to intval(), resulting in a new array of integers.

The output results are as follows:

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

We can see that the original string array has been successfully converted into an integer array.

In addition to array_map, PHP also provides another function array_walk, which can also accomplish the same operation. The difference is that array_walk passes the original array to the callback function by reference, so the element values ​​in the original array can be modified directly.

The sample code is as follows:

$arr = array("1", "2", "3", "4");

array_walk($arr, function(&$value) {
    $value = intval($value);
});

print_r($arr);

In the above code, we set the callback function to intval(), which can convert a string into an integer. So array_walk will walk through each element in the $arr array and pass it to intval(), getting a new integer value.

The output result is as follows:

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

We can see that the original array $arr has been successfully converted into an integer array.

To sum up, converting a string array to an integer array is a relatively simple operation. We can use the array_map or array_walk function to accomplish this. It should be noted that the intval() function can convert a string into an integer, so it is a very useful tool.

The above is the detailed content of Convert php string array to int. 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
Previous article:php array convert sqlNext article:php array convert sql