错误的结果:两个变量的总和

I have a simple problem, but I can't solve it, still.

Working code

(int)$sum_price=4;
(int)$price_per_sum = (int)$spo[$key]*(int)$gpp['price'];
echo $sum_price = ($sum_price + $price_per_sum);

When I change 4 to a variable, the result of sum() is string.

Result becomes a string

 $spo[$key] = 1;
 $gpp['price'] = 2;
(int)$sum_price=4;
(int)$price_per_sum = (int)$spo[$key]*(int)$gpp['price'];
 echo $sum_price = ($sum_price + $price_per_sum);

The result is 42 but should be 6, instead. Using (int) does not change my result.

Problem code

# Get All Payment 
(int)$sum_price = 0;
$sum_price_product = explode('|',$_SESSION['product']);
$spo = explode('|',$_SESSION['order']);
foreach($sum_price_product as $key=>$spp)
{
  if($spp!='')
  {
    $get_product_price = $dbc->select("sh_product"," id = '{$spp}'","id");
    $gpp = mysql_fetch_array($get_product_price['sql']);
    (int)$price_per_sum = $spo[$key] * $gpp['price'];
    $sum_price = $sum_price + $price_per_sum;
  echo $sum_price;
  }
}

http://codepad.org/Zo9X2PY5

Casting is not required in this case. Please review below code and try it out,its working for me:

$spo[0] = 1;
$gpp['price'] = 2;
$sum_price=4;
$price_per_sum = $spo[0]*$gpp['price'];
$sum_price = $sum_price + $price_per_sum;
echo $sum_price;

Here in $spo[0], i have considered single value but you can replace '0' by $key in loop construct.

Please review here my working code: http://codepad.org/zH2q2WjH

Type Casting is not necessary in this case... And please put the right associative array key its "price"or 'price' not just price...

$sum_price = 4;
$price_per_sum = $spo[$key] * $gpp['price'];
$sum_price = ($sum_price + $price_per_sum);
echo $sum_price;

And your Example :

$spo[$key] = 1;
$gpp['price'] = 2;
$sum_price = 4;
$price_per_sum = $spo[$key] * $gpp['price'];
$sum_price = ($sum_price + $price_per_sum);
echo $sum_price;

# Get All Payment 
(int)$sum_price=0;
$sum_price_product = explode('|',$_SESSION['product']);
$spo = explode('|',$_SESSION['order']);
foreach($sum_price_product as $key=>$spp)
{
if($spp!='')
{
$get_product_price = $dbc->select("sh_product"," id = '{$spp}'","id");
$gpp = mysql_fetch_array($get_product_price['sql']);
(int)$price_per_sum = $spo[$key]*$gpp['price'];
$sum_price = $sum_price + $price_per_sum;
echo $sum_price;
}
}