使用simplexml_load_file()对象进行乘法不起作用

时间:2014-12-14 20:12:16

标签: php xml for-loop foreach multiplication

我使用XML

PHP中提取值
<?php 

    $url = 'list.xml';
    $xml = simplexml_load_file($url);

    $entries = $xml->item;
    $i = 0;
    $total = 1;

    foreach($entries as $entry){
        $i++;
        $number[$i] = $entry->total;
        $total *= $number[$i];
    }

    echo $total;

?> 

如何根据从XML中提取的每个$编号构建总计?现在,我的总数是零。

所以对于所有循环,例如:

$total = $number[1] * $number[2] * $number[3] * $number[4] ....

1 个答案:

答案 0 :(得分:3)

这应该适合你:

(您必须将simplexml_load_file()的回报转换为double

$url = "list.xml"; 
$xml = simplexml_load_file($url); 

$entries = $xml->results->rate; 
$count = 0; 
$total = 1; 
$number = array(); 

foreach($entries as $entry){ 
    $count++; 
    $number[$count] = $entry->Bid; 
    $total *= (double)$number[$count]; 
} 

echo "Total: " . $total;