sum项目存储在数组中

时间:2015-02-12 14:14:32

标签: arrays sum

我有这段代码:

$str='ياسمين';

$a=utf8Split(trim($str));
for($i=0;$i<=strlen($a);$i++)
{

$sql_char=("SELECT num FROM alphabet where letters = '$a[$i]'");
$result = mysql_query ($sql_char,$dbConn);

while($row = mysql_fetch_array($result))
echo  $row['num'].'<br>';

}

1 个答案:

答案 0 :(得分:0)

只需将$row['num']添加到新的variable

即可
$sum=0;
while($row = mysql_fetch_array($result)){
echo  $row['num'].'<br>';
$sum=$sum+$row['num'];
}
echo $sum; //171

修改

function mb_str_split( $string ) {
    # Split at all position not after the start: ^
    # and not before the end: $
    return preg_split('/(?<!^)(?!$)/u', $string );
}

$str   = 'ياسمين';
$charlist = mb_str_split( $str );

print_r( $charlist );