以形式KE00033-00获取memberID,其中最后两位数表示成员系列中的个人数。因此,我们能够获得所有memberID并将其爆炸,但无法对它们进行总结。 array_sum()输出错误的值。这是代码:
$query=mysql_query("SELECT * FROM subscribers");
$arr = array();
while($row=mysql_fetch_array($query)){
$arr[] =$row['member'];
}
foreach ($arr as $value)
{
$entries=explode('-',$value);
print_r($entries)."<br>";//outputs the last two digits of memberID's
echo array_sum($entries);
}
答案 0 :(得分:1)
There is no need of extra foreach loop there. Try this.
$query=mysql_query("SELECT * FROM subscribers");
$arr = array();
$individual = array();
while($row=mysql_fetch_array($query)){
$arr =explode('-', $row['member']);
$individual[] = $arr[1];
}
echo array_sum($individual);
}
答案 1 :(得分:0)
使用explode
会为您提供一个数组,其中包含将需求值$entries[1]
再次转换为数组所需的值,以便将数字转换为sum
$query=mysql_query("SELECT * FROM subscribers");
$arr = array();
while($row=mysql_fetch_array($query)){
$arr[] =$row['member'];
}
$individuals=0;
foreach ($arr as $value)
{
$entries=explode('-',$value);
print_r($entries[1])."<br>";//outputs 00 now you need to make array for this no to sum up
$arr1 = str_split($entries[1]);
// Array
//(
// [0] => 0
// [1] => 0
//)
//Use array_sum to sum up the array values
echo array_sum($arr1);
}
或者如果您想获取所有记录的总和,您可以使用+=
$individuals=0;
foreach ($arr as $value)
{
$entries=explode('-',$value);
$individuals+= $entries[1];
}
echo $individuals;