Php 如何从@attribute部分获取值?

Php 如何从@attribute部分获取值?,php,Php,我无法从该数组中获取这些值。我需要知道代码和名称,这样我的应用程序就知道应该选择哪个,但我无法从中获取值。有人能帮我吗?它是@attributes中的值 顺便说一下,我正在使用PHP 谢谢 array(2) { [0]=> object(SimpleXMLElement)#22 (2) { ["@attributes"]=> array(2) { ["code"]=>

我无法从该数组中获取这些值。我需要知道代码和名称,这样我的应用程序就知道应该选择哪个,但我无法从中获取值。有人能帮我吗?它是@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"
      }
    }

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

例如,给定以下代码:

$xml = new SimpleXMLElement('<root><item foo="bar"></item></root>');
但是,有一个问题:返回的值实际上是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

我自己想出来的

$xmlResponse->AccessorialCharges->OtherAccessorialCharges[$i]['code'];

使用
attributes()
方法。请看这个问题:很抱歉无知,但我真的不知道从哪里开始。是否可以参考我发布的代码编写,或者在这种情况下需要查看所有XML?
$xmlResponse->AccessorialCharges->OtherAccessorialCharges[$i]['code'];