将 XML 解析为数组 [英] Parsing XML into Array

查看:27
本文介绍了将 XML 解析为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个 XML 结构:

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?

推荐答案

$xml = simplexml_load_string($xml);
$fields = array();
foreach ($xml->field as $f) {
    $f = (array) $f->attributes();
    $fields[] = $f['@attributes'];
}

这篇关于将 XML 解析为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆