0

我无法弄清楚从这个数组中获取这些值。我需要知道代码和名称,以便我的应用程序知道该选择哪一个,但我无法从中获取值。有人可以帮我吗?这是@attributes 中的值。

顺便说一句,我正在使用 PHP。

谢谢

              array(2) {
      [0]=>
      object(SimpleXMLElement)#22 (2) {
        ["@attributes"]=>
        array(2) {
          ["code"]=>
          string(3) "HCD"
          ["name"]=>
          string(31) "HIGH COST DELIVERY REGION SRCHG"
        }
        [0]=>
        string(5) "71.25"
      }
      [1]=>
      object(SimpleXMLElement)#24 (2) {
        ["@attributes"]=>
        array(2) {
          ["code"]=>
          string(3) "HCD"
          ["name"]=>
          string(31) "HIGH COST DELIVERY REGION SRCHG"
        }
        [0]=>
        string(5) "71.25"
      }
    }
4

2 回答 2

0

我自己想通了。

$xmlResponse->AccessorialCharges->OtherAccessorialCharges[$i]['code'];
于 2013-10-30T21:03:45.960 回答
0

使用 SimpleXML,您可以像使用数组一样使用元素($xml->elementName['attributeName']或使用->attributes()前面给出的方法)来访问 XML 上的属性。

例如,给定以下代码:

$xml = new SimpleXMLElement('<root><item foo="bar"></item></root>');

如果我想访问fooitem 元素的属性,我会像这样访问它:

echo $xml->item['foo'];

但是,有一个问题:返回的值实际上是 SimpleXMLElement 的一个实例。为了能够存储或使用它,您需要将其转换为原始类型:

echo (string)$xml->item['foo']; // now a string
echo (int)$xml->item['foo']; // now an integer
echo (bool)$xml->item['foo']; // now a boolean
于 2013-10-30T20:03:03.867 回答