bart2puck
bart2puck

Reputation: 2522

How do i parse xml to get the value of the child element

The Xml data I have is:

<?xml version="1.0" encoding="UTF-8"?>
<var>
  <packid>27441</packid>
  <clientid>1426</clientid>
  <desc>PO: 9005969</desc>
  <planned_activation_date>0</planned_activation_date>
</var>

I am trying to get the values of each child.

            $xml = new \SimpleXMLElement($bef);
            foreach ($xml->children() as $child) {
                
                $event[$someId][$child->getName()] = $child;
            }
           print_r($event);

This results in

Array
(
    [1147037] => Array
        (
                [packid] => SimpleXMLElement Object
                    (
                        [0] => 27456
                    )

                [clientid] => SimpleXMLElement Object
                    (
                        [0] => 1474
                    )

                [desc] => SimpleXMLElement Object
                    (
                        [0] => Colocation Fiber Cross Connect (PO# 75122)_--Setup Fee
                    )

                [planned_activation_date] => SimpleXMLElement Object
                    (
                        [0] => 0
                    )
    )
)

What do I need to do to get:

Array
(
    [1147037] => Array
    (
                [packid] => 27456
                [clientid] => 1474
                [desc] => Colocation Fiber Cross Connect (PO# 75122)_--Setup Fee
                [planned_activation_date] => 0
    )
)

Upvotes: -1

Views: 25

Answers (1)

bart2puck
bart2puck

Reputation: 2522

The answer as noted by @u_mulder is to use typecast:

(string) $child

Upvotes: 0

Related Questions