Home >Backend Development >PHP Problem >How to convert values in an array to numbers in php
In PHP development, we usually need to perform numerical conversion on the values in the array. Especially during data processing and arithmetic operations, we need to ensure that the values in the array are all numeric types. This article will introduce several ways to convert values in PHP arrays to numeric types.
1. Use (int) to cast
In PHP, you can use (int) to cast a variable to an integer type. Likewise, we can also use it to force the values in an array to be converted to numeric type. The following is a sample code:
$array = array('1', '2', '3'); foreach ($array as $key => $value) { $array[$key] = (int) $value; } var_dump($array);
The output result is:
array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
The above code will traverse the array$array
and use cast to convert each array element to an integer type.
It should be noted that cast type conversion can only convert strings to numeric types. If the elements in the array are other types of data (such as Boolean, floating point, etc.), the cast will fail.
2. Use the intval() function to convert
PHP provides an intval()
function to convert variables to integer types. Different from forced type conversion, intval()
can convert variables of various types such as strings, floating point numbers, and Boolean values into integer types. The following is a sample code:
$array = array('1', '2.5', true, false, '5'); foreach ($array as $key => $value) { $array[$key] = intval($value); } var_dump($array);
The output result is:
array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(1) [3]=> int(0) [4]=> int(5) }
In the above code, use the intval()
function to convert each element in the array to an integer type . Since the rules for converting Boolean values to integers are true
is converted to 1
and false
is converted to 0
, so in the output result, the Three elements are converted to 1
, and the fourth element is converted to 0
.
It should be noted that when using the intval()
function, if the parameter is empty or cannot be converted to an integer type, 0 will be returned.
3. Use the array_map() function to convert
In addition to the for loop and foreach loop, you can also use the array_map()
function to convert the elements in the array into numbers. type. array_map()
The function can process each element in the incoming array through the callback function and return the processed new array. The following is a sample code:
$array = array('1', '2', '3'); $array = array_map('intval', $array); var_dump($array);
The output result is:
array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
The above code passes the intval()
function as a callback function to array_map()
function. array_map()
will iterate through each element and call the callback function on it, and finally return the new processed array. In this case, array_map()
will return an array with each string element converted to an integer type.
It should be noted that the array_map()
function does not change the original array, but returns a new array. If you want to change the original array, you need to assign the new array back to the original array.
Summary
This article introduces three methods to convert values in PHP arrays to numeric types. You can easily convert elements in an array to a numeric type, whether using a cast, the intval()
function, or the array_map()
function. During the development process, just choose the appropriate method according to actual needs.
The above is the detailed content of How to convert values in an array to numbers in php. For more information, please follow other related articles on the PHP Chinese website!