我有一个如下所示的 XML 文件:
<services>
<service>
<code>MODULE_1</code>
<name>Regular</name>
<price>14.85</price>
</service>
<service>
<code>MODULE_2</code>
<name>Express</name>
<price>22.50</price>
</service>
</services>
$result
从另一台服务器检索文件。我的代码如下所示:
$methods = array() ;
$xmlQuotes = new SimpleXMLElement($result) ;
foreach($xmlQuotes as $quote) {
$add = 0 ; $f = 0 ;
switch ($quote->code) {
case "MODULE_1" ;
if ((in_array("Regular", $this->allowed_methods))) {
$add = MODULE_1_HANDLING ; $f = 1 ;
$id = "m_1";
$cost = $quote->price;
$description = $quote->name;
}
break;
case "MODULE_2" ;
if ((in_array("Express", $this->allowed_methods))) {
$add = MODULE_2_HANDLING ; $f = 1 ;
$id = "m_2";
$cost = $quote->price;
$description = $quote->name;
}
break;
}
if (($cost > 0) && ($f == 1)) {
$cost = $cost + $add ;
$methods[] = array('id' => "$id",
'title' => "$description",
'cost' => "$cost");
}
}
$this->quotes['methods'] = $methods;
这一切都很好,直到最后一点 - 当我使用代码时,$methods
数组不会返回正确的$cost
并且$description
它是匹配的$code
,而是返回我的第一个模块的价格和描述。它看起来像这样:
MODULE_1: Regular: 14.85
MODULE_2: Regular: 14.85
我在这里疯了,因为我显然不知道我在做什么,所以任何帮助将不胜感激。