解析XML到数组 [英] Parsing XML into Array

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

问题描述

所以我有这样的XML结构:

 <&领域GT;
    <字段名=agent_description标签=代理说明大小=区域/>
    <字段名=agent_phone标签=中介电话大小=田/>
    <字段名=AGENT_EMAIL标签=发送电子邮件大小=田/>
    <字段名=agent_listing_url标签=代理列表大小=田/>
    <字段名=profile_video_title标签=自拍视频标题大小=田/>
    <字段名=profile_video_sub_title标签=自拍视频副标题大小=田/>
    <字段名=profile_video_url标签=自拍视频(YouTube)的大小=田/>< /田>

我想解析到一个数组结构,看起来像这样的:

 阵列(
            阵列(名= gt;中agent_description
                标签=> 代理说明
                大小=> 区),            阵列(名= gt;中agent_phone
                标签=> 中介电话
                大小=> 领域),            阵列(名= gt;中AGENT_EMAIL
                标签=> 发送电子邮件
                大小=> 领域),            阵列(名= gt;中agent_listing_url
                标签=> 代理列表,
                大小=> 领域),            阵列(名= gt;中profile_video_title
                标签=> 自拍视频标题
                大小=> 领域),            阵列(名= gt;中profile_video_sub_title
                标签=> 自拍视频副标题
                大小=> 领域),            阵列(名= gt;中profile_video_url
                标签=> 自拍视频(YouTube)的
                大小=> 领域),

最新最好的方式做到这一点?


解决方案

  $ XML = simplexml_load_string($ XML);
$栏=阵列();
的foreach($ XML的>字段$ F){
    $ F =(阵列)$ F->属性();
    $域[] = $ F ['@属性'];
}

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天全站免登陆