Php 字符串中的操作值

Php 字符串中的操作值,php,arrays,string,foreach,operation,Php,Arrays,String,Foreach,Operation,我用PHP编写了以下代码: $arreglo = array('128 gigas', '250 gigas', '220 gigas'); foreach ($arreglo as $key => $value) { } 是否可以在字符串中操作这些值?比如128+250+220,使用foreach? 提前谢谢。如果字符串始终遵循该格式,则选择“是”。可以将字符串分解为数组: $a = explode(' ', $string); // Now $a[0] contains the n

我用PHP编写了以下代码:

$arreglo = array('128 gigas', '250 gigas', '220 gigas');
foreach ($arreglo as $key => $value) {

}
是否可以在字符串中操作这些值?比如128+250+220,使用foreach?
提前谢谢。

如果字符串始终遵循该格式,则选择“是”。可以将字符串分解为数组:

$a = explode(' ', $string); // Now $a[0] contains the number
因此,对于您的代码:

$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
}
或者,如果你觉得有创意:

$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);

如果字符串始终遵循该格式,则为“是”。可以将字符串分解为数组:

$a = explode(' ', $string); // Now $a[0] contains the number
因此,对于您的代码:

$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
}
或者,如果你觉得有创意:

$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);
使用以下代码:

$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$arr = array();
$i = 0;
foreach ($arreglo as $key => $value) 
{
   $expVal = explode(" ",$vaulue);
   $arr[$i] = $expVal[0]; //this array contains all your numbers 128, 250 etc 
}
$sum = 0;
foreach($arr[$i] as $num)
{
    $sum = $sum+$num
}
echo $sum; // your final sum result is here
使用以下代码:

$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$arr = array();
$i = 0;
foreach ($arreglo as $key => $value) 
{
   $expVal = explode(" ",$vaulue);
   $arr[$i] = $expVal[0]; //this array contains all your numbers 128, 250 etc 
}
$sum = 0;
foreach($arr[$i] as $num)
{
    $sum = $sum+$num
}
echo $sum; // your final sum result is here

您可以使用基本的PHP函数:

工作示例:

<?php
$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$str = implode(',', $arreglo);
$str = str_replace(' gigas', '', $str);
$n = explode(',', $str);
$count = array_sum($n);
echo $count; // Outouts 598
?>

您可以使用基本的PHP函数:

工作示例:

<?php
$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$str = implode(',', $arreglo);
$str = str_replace(' gigas', '', $str);
$n = explode(',', $str);
$count = array_sum($n);
echo $count; // Outouts 598
?>

如果数字和字母之间的空格始终存在,您可以这样做

$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$total = 0;
foreach ($arreglo as $value) {
    $total += strstr($value, " ", true);
}
echo "Total : $total"; // int(598)

如果数字和字母之间的空格始终存在,则可以执行此操作

$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$total = 0;
foreach ($arreglo as $value) {
    $total += strstr($value, " ", true);
}
echo "Total : $total"; // int(598)
试试这个

<?php
    $total=0;
    $arreglo = array('128 gigas', '250 gigas', '220 gigas');
foreach ($arreglo as $key => $value) {
$total +=intval(preg_replace('/[^0-9]+/', '', $value), 10);
}
echo $total;
    ?>

演示:

试试这个

<?php
    $total=0;
    $arreglo = array('128 gigas', '250 gigas', '220 gigas');
foreach ($arreglo as $key => $value) {
$total +=intval(preg_replace('/[^0-9]+/', '', $value), 10);
}
echo $total;
    ?>

演示:

您可以使用

工作示例:

<?php
$data = array('128 gigas', '250 gigas', '220 gigas');
$data = array_map(function($value) { return str_replace(' gigas', '', $value); }, $data);
echo array_sum($data);
?>

说明:

1) 您有一个具有字符串值的数组。这些值也包含数字

2) 你需要把所有的数字加起来

3) 使用
str\u replace()
替换字符串中的所有非数字字符

4) 现在,使用array_sum()对总计进行求和。

您可以使用,和

工作示例:

<?php
$data = array('128 gigas', '250 gigas', '220 gigas');
$data = array_map(function($value) { return str_replace(' gigas', '', $value); }, $data);
echo array_sum($data);
?>

说明:

1) 您有一个具有字符串值的数组。这些值也包含数字

2) 你需要把所有的数字加起来

3) 使用
str\u replace()
替换字符串中的所有非数字字符


4) 现在,使用array_sum()对总计进行求和。

如果需要内联代码

echo array_sum(str_replace(" gigas","",$arreglo));

如果你想要内联代码

echo array_sum(str_replace(" gigas","",$arreglo));

您可以简单地使用floatval()php函数从字符串中获取浮点值。希望这有帮助

$arreglo = array('128 gigas', '250 gigas', '220 gigas');

$sum=0;
foreach( $arreglo as $value ){
    $sum += floatval($value);
}

print $sum;

您可以简单地使用floatval()php函数从字符串中获取浮点值。希望这有帮助

$arreglo = array('128 gigas', '250 gigas', '220 gigas');

$sum=0;
foreach( $arreglo as $value ){
    $sum += floatval($value);
}

print $sum;
试试这个

<?php
$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$sum=0;
foreach ($arreglo as $key => $value) {
$sum+=(int)$value;
}
echo $sum;
?>

试试这个

<?php
$arreglo = array('128 gigas', '250 gigas', '220 gigas');
$sum=0;
foreach ($arreglo as $key => $value) {
$sum+=(int)$value;
}
echo $sum;
?>