I have this in PHP:
$arreglo = array('128 gigas', '250 gigas', '220 gigas');
foreach ($arreglo as $key => $value) {
}
Is it possible to operate these values in the string? like 128 + 250 + 220, using foreach ? Thank you in advance.
I have this in PHP:
$arreglo = array('128 gigas', '250 gigas', '220 gigas');
foreach ($arreglo as $key => $value) {
}
Is it possible to operate these values in the string? like 128 + 250 + 220, using foreach ? Thank you in advance.
If the strings always follow that format, then yes. You could explode the strings into arrays:
$a = explode(' ', $string); // Now $a[0] contains the number
So, for your code:
$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$total = 0;
foreach ($arreglo as $value) { // $key not necessary in this case
$a = explode(' ', $value);
$total += $a[0]; // PHP will take care of the type conversion
}
Or, if you are feeling creative:
$func = function($s) {
$a = explode(' ', $s);
return $a[0];
};
$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$numbers = array_map($func, $arreglo);
$total = array_sum($numbers);