0

I cannot figure out to get these values from this array. I need to know the code and name so my application knows which one to go for but I can't get the values out of there. Can someone please help me out? It's the values in the @attributes.

I'm using PHP by the way.

Thanks

              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"
      }
    }
smack-a-bro
  • 691
  • 2
  • 13
  • 27
  • Using the `attributes()` method. See this question: [Accessing @attribute from SimpleXML](http://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) – Amal Murali Oct 30 '13 at 19:12

2 Answers2

0

Using SimpleXML, you're able to access the attributes on an XML by using the elements as if it was an array ($xml->elementName['attributeName'], or using the ->attributes() method as previously given).

For example, given the following code:

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

If I wanted to access the foo attribute on the item element, I would access it like this:

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

However, there's a catch: the value that is returned is actually an instance of SimpleXMLElement. To be able to store or use it, you will need to convert it to a primitive type:

echo (string)$xml->item['foo']; // now a string
echo (int)$xml->item['foo']; // now an integer
echo (bool)$xml->item['foo']; // now a boolean
garbetjie
  • 579
  • 3
  • 10
  • Sorry for being ignorant but I really don't know where to start. Is it possible to write that in reference to the code I posted or is this a situation where you would need to see all the XML? – smack-a-bro Oct 30 '13 at 20:10
0

I figured it out on my own.

$xmlResponse->AccessorialCharges->OtherAccessorialCharges[$i]['code'];
smack-a-bro
  • 691
  • 2
  • 13
  • 27