dongpin3794 2016-03-17 20:02
浏览 385
已采纳

array_sum以字符串形式返回值的总和

This seems like it should be really straightforward, but I keep getting unexpected output. I'm trying to access specified rows in a SQL database which each contain a numerical value and then calculate the sum of those values. PHP is concatenating the values as if they were strings even after I've set the datatype of the values to float. My code:

$query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
$query .= "AND city_name = 'city'"; 
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result)) {
$population_value = $row['population_value'];

$population_value = is_array($population_value) ? $population_value : array($population_value);

foreach($population_value as $value){
echo $value;
}       

echo array_sum($population_value);
}

I have also tried:

$total = array("");

foreach($population_value as $value){
floatval($value);
array_push($total, $value);
echo $value;
}

echo array_sum($total);

My output is always something like: 100002000030000 with 10,000 20,000 and 30,000 being the values of each population.

I've successfully calculated sums using foreach with values that weren't retrieved from MySQL. What is going on here?

  • 写回答

2条回答 默认 最新

  • dongqian1893 2016-03-17 20:15
    关注
    $query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
    $query .= "AND city_name = 'city'"; 
    $result = mysqli_query($connection, $query);
    
    while($row = mysqli_fetch_assoc($result)) {
    $population_value = $row['population_value']; 
    
    //This is actually rewriting the array or NOT adding the value to it.
    $population_value = is_array($population_value) ? $population_value : array($population_value);
    
    //ok, so you're going to repeatedly output this?
    foreach($population_value as $value){
    echo $value; 
    }       
    
    echo array_sum($population_value);
    }
    

    I think what you want is this:

    $query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
    $query .= "AND city_name = 'city'"; 
    $result = mysqli_query($connection, $query);
    
    $population_value=array(); //Initialize the array so we can just add to it.
    
    while($row = mysqli_fetch_assoc($result)) {
        $population_value[]= intval($row['population_value']); //Just making sure we get a number. 
    
    
        echo end($population_value); //We could output the row again, or just grab the last element of the array we added.
    
    }
    //Now that the array is fully populated and we've made sure it's only numbers, we output the grand total.
    echo array_sum($population_value);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输