php is a widely used server-side scripting language. It has always been loved by developers because it is easy to learn, use and develop quickly. In the PHP language, processing arrays is a very common operation, and it has very powerful array functions to operate arrays. However, when performing array operations, we often encounter situations where we need to convert strings in the array into numbers. This article is to introduce how to convert strings in the array into numbers in PHP.
There are many ways to convert strings to numbers in php, the main methods are as follows:
1. Use the intval function to convert strings to integers
The intval() function is a built-in function in PHP, mainly used to convert a string into an integer.
For example, we have an array a, and all the elements in the array are strings:
$a = array('1', 2, '3', '4');
If you want to convert all the strings in this array to integers, you can use the following code :
foreach ($a as &$value) { $value = intval($value); }
In this way, we can convert all the string elements in the array $a to integers.
2. Use the floatval function to convert a string to a floating point type
Similar to the intval() function, the floatval() function is also a built-in function in php, used to convert a string Convert to floating point type.
For example, we have an array b, and all the elements in the array are strings:
$b = array('1.1', '1.2', '3.3', '4.4');
If you want to convert all the strings in this array to floating point, you can use the following Code:
foreach ($b as &$value) { $value = floatval($value); }
In this way, we can convert all the string elements in the array $b to floating point types.
3. Use forced type conversion
Forced type conversion in php can also convert a string to a numeric type. For example, we have an array c, and all the elements in the array are strings:
$c = array('1', '2', '3', '4');
If you want to convert all the strings in this array to integers, you can use the following code:
foreach ($c as &$value) { $value = (int)$value; }
In this way we can convert all string elements in array $c to integers.
4. Use mathematical operators such as addition, subtraction, multiplication, and division.
The mathematical operators in php can also convert a string into a numeric type. For example, we have an array d, and all the elements in the array are strings:
$d = array('1', '2', '3', '4');
If you want to convert all the strings in this array to integers, you can use the following code:
foreach ($d as &$value) { $value = $value + 0; }
In this way we can convert all string elements in array $d to integers.
Summary
It is not difficult to convert a string into a number in PHP. There are four main methods, namely using the intval() function to convert the string into an integer type and using floatval( ) function converts a string to a floating-point type, uses cast to convert a string to a numeric type, and uses mathematical operators such as addition, subtraction, multiplication, and division to convert a string to a numeric type. Just choose the appropriate method according to your needs.
The above is the detailed content of How to convert strings in array to numbers in php. For more information, please follow other related articles on the PHP Chinese website!