0

So i have this XML structure:

<fields>
    <field name="agent_description" label="Agent Description" size="area" />
    <field name="agent_phone" label="Agent Phone" size="field" />
    <field name="agent_email" label="Agent EMail" size="field" />
    <field name="agent_listing_url" label="Agent Listing" size="field" />
    <field name="profile_video_title" label="Profile Video Title" size="field" />
    <field name="profile_video_sub_title" label="Profile Video Sub Title" size="field" />
    <field name="profile_video_url" label="Profile Video (YouTube)" size="field" />

</fields>

i would like to parse this into an array structure that looks like this:

array(
            array("name" => "agent_description", 
                "label" => "Agent Description",
                "size" => "area"),

            array("name" => "agent_phone", 
                "label" => "Agent Phone",
                "size" => "field"),

            array("name" => "agent_email", 
                "label" => "Agent EMail",
                "size" => "field"),

            array("name" => "agent_listing_url", 
                "label" => "Agent Listing",
                "size" => "field"),

            array("name" => "profile_video_title", 
                "label" => "Profile Video Title",
                "size" => "field"),

            array("name" => "profile_video_sub_title", 
                "label" => "Profile Video Sub Title",
                "size" => "field"),

            array("name" => "profile_video_url", 
                "label" => "Profile Video (YouTube)",
                "size" => "field"),
)

whats the best way to accomplish this?

2 Answers 2

4
$xml = simplexml_load_string($xml);
$fields = array();
foreach ($xml->field as $f) {
    $f = (array) $f->attributes();
    $fields[] = $f['@attributes'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not all servers have SimpleXML enabled.
2
$dom = new DOMDocument;
$dom->loadXML($xml);
$fields = $dom->getElementsByTagName('field');
$arr = array();
foreach ($fields as $field) {
  $arr[] = array(
    'name' => $field->getAttribute('name'),
    'label' => $field->getAttribute('label');
    'size' => $field->getAttribute('size'),
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.