To convert a set of strings to a set of floats in PHP, you can use the array_map()
function along with the floatval()
function to transform each string value into its corresponding float value.
In this example,
$stringArray1
with values ["1.1", "2.2", "3.3"]
.array_map()
function on $stringArray1
to transform each string value into its corresponding float value. We pass the floatval()
function as the callback to array_map()
.$floatArray1
.array_unique()
function to remove duplicate values.<?php
$stringArray1 = ["1.1", "2.2", "3.3"];
$floatArray1 = array_unique(array_map("floatval", $stringArray1));
print_r($floatArray1);
?>
[1.1, 2.2, 3.3]
In this example,
$stringArray2
with values ["4.5", "5.6", "6.7"]
.array_map()
function on $stringArray2
to transform each string value into its corresponding float value. We pass the floatval()
function as the callback to array_map()
.$floatArray2
.array_unique()
function to remove duplicate values.<?php
$stringArray2 = ["4.5", "5.6", "6.7"];
$floatArray2 = array_unique(array_map("floatval", $stringArray2));
print_r($floatArray2);
?>
[4.5, 5.6, 6.7]
In this tutorial, we learned How to Convert a Set of Strings to a Set of Floats in PHP language with well detailed examples.