XML转换成使用PHP关联数组 [英] XML into Associative Array using PHP

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

问题描述

任何人都可以从XML文档中的数据转换成一个关联数组帮助吗?我快到鉴于XML结构是那种3D的问题和数组更多的是一种二维结构(请原谅各地我缺乏正确的术语)。
XML元素有属性,儿童和祖父母儿童(但我从来不知道他们的名字),所以我想我会尽量让按键阵列中的每个孩子的串联/属性名和属性等于价值,那么,价值。麻烦的是我需要的属性名和值作为串联阵列关键的一部分,使其独特的...

例如:

 <计算机ID =1>
    < OS>
        <名称> Linux和LT; /名称>
        <年龄>比我&LT较早; /年龄>
    < / OS>
< /计算机与GT;
&所述;计算机的id =2>
    < OS>
        <名称> Windows和LT; /名称>
        <年龄>与其说< /年龄>
    < / OS>
< /计算机与GT;

最好应放弃:

  [电脑-ID-1-OS-名称] ='Linux的
[电脑-ID-1-OS-年龄] ='比我大
[电脑-ID-2-OS-名称] ='视窗'
[电脑-ID-2-OS-年龄] ='与其说'

但我得到这个结果:

  [电脑-ID] ='1'
[电脑-OS-名称] ='Linux的
[电脑-OS-年龄] ='比我大
[电脑-ID] ='2'
[电脑-OS-名称] ='视窗'
[电脑-OS-年龄] ='与其说'

这样[计算机-ID]键不唯一。我使用的是递归函数中的值阅读,但我想不出如何获取属性名称和属性值的从属键的名字......(顺便有一个很好的理由这样做看似不合逻辑的任务!)
任何帮助将大大AP preciated ...

下面是'变平它已经被读入一个多维阵列中的XML数据后的功能。我不知道我要对这个正确的方式!

 函数flattenArray($数组$ baseName的= NULL)
{
    复位($数组);
    而(名单($键,$值)=每($阵列)){
        $ outKey = $键。 - ;
        如果(is_array($值)){
            flattenArray(价值$,$ $ baseName的outKey);
        }其他{
            $ finalKey = $ baseName的。 RTRIM($ outKey,' - ');
            $ finalValue = $价值;
            回声$ finalKey = $ finalValue \\ N的;
        }
    }
}


解决方案

一个例子可能是:

  $ DOM =新的DOM文档;
$ dom->的loadXML(
    '<根和GT;
        &所述;计算机的id =1>
            < OS>
                <名称> Linux和LT; /名称>
                <年龄>比我&LT较早; /年龄>
            < / OS>
        < /计算机与GT;        &所述;计算机的id =2>
            < OS>
                <名称> Windows和LT; /名称>
                <年龄>与其说< /年龄>
            < / OS>
        < /计算机与GT;
    < /根>'
);$ = XPath的新DOMXPath($ DOM);
$结果=阵列();的foreach($ xpath->查询('// * [COUNT(*)= 0]')为$节点){
    $ PATH =阵列();
    $ VAL = $&于节点GT;的nodeValue;    做{
        如果($于节点> hasAttributes()){
            的foreach($&于节点GT;属性为$属性){
                $ PATH [] = sprintf的(%S [%S]',$属性 - >节点名称,属性 - $>的nodeValue);
            }
        }
        $ PATH [] = $&于节点GT;节点名称;
    }
    而($节点= $&于节点GT; parentNode);    $结果[破灭('/',array_reverse($ PATH))] = $ VAL;
}的print_r($结果);

输出:

 阵列

    [#文件/根/计算机/ ID [1] / OS /名称] => Linux的
    [#文件/根/计算机/ ID [1] / OS /年龄=>年纪比我大
    [#文件/根/计算机/ ID [2] / OS /名称] =>视窗
    [#文件/根/计算机/ ID [2] / OS /年龄=>没那么多

那不是正是你要找的,但它是一个开始,可以方便地调整,以得到不同的结果。

Can anyone help with converting data from an XML document into an associative array? I'm running into issues given that the XML structure is sort of 3D and the array is more of a 2D structure (please forgive my lack of correct terminology throughout). The XML elements have attributes, children and grand-children (but I never know their names), so I figured I'd try to make the key in the array a concatenation of each child/attribute name and the value equal to, well, the value. Trouble is I need the attribute name and value as part of the concatenated array key to make it unique...

For example:

<Computer id="1">   
    <OS>
        <Name>Linux</Name>
        <Age>Older than me</Age>
    </OS>
</Computer>
<Computer id="2">
    <OS>
        <Name>Windows</Name>
        <Age>Not so much</Age>
    </OS>
</Computer>

Should ideally give:

[Computer-id-1-OS-Name] = 'Linux'
[Computer-id-1-OS-Age] = 'Older than me'
[Computer-id-2-OS-Name] = 'Windows'
[Computer-id-2-OS-Age] = 'Not so much'

But I'm getting this result:

[Computer-id] = '1'
[Computer-OS-Name] = 'Linux'
[Computer-OS-Age] = 'Older than me'
[Computer-id] = '2'
[Computer-OS-Name] = 'Windows'
[Computer-OS-Age] = 'Not so much'

So that the [Computer-id] key is not unique. I'm using a recursive function to read in the values, but I can't figure how to get the attribute name and attribute value into the name of the subordinate keys...(By the way there is a good reason for doing this seemingly illogical task!) Any help would be greatly appreciated...

Here is the function which 'flattens' the XML data after it has been read into a multi-dimensional array. I'm not sure I'm going about this the right way!

function flattenArray ($array, $baseName = NULL)
{
    reset($array);
    while (list ($key, $value) = each($array)) {
        $outKey = $key . "-";
        if (is_array($value)) {
            flattenArray($value, $baseName . $outKey);
        } else {
            $finalKey = $baseName . rtrim($outKey, '-');
            $finalValue = $value;
            echo "$finalKey = $finalValue\n";
        }
    }
}

解决方案

One example could be:

$dom = new DOMDocument;
$dom->loadXML(
    '<root>
        <Computer id="1">   
            <OS>
                <Name>Linux</Name>
                <Age>Older than me</Age>
            </OS>
        </Computer>

        <Computer id="2">
            <OS>
                <Name>Windows</Name>
                <Age>Not so much</Age>
            </OS>
        </Computer>
    </root>'
);

$xpath = new DOMXPath($dom);
$result = array();

foreach ($xpath->query('//*[count(*) = 0]') as $node) {
    $path = array();
    $val = $node->nodeValue;

    do {
        if ($node->hasAttributes()) {
            foreach ($node->attributes as $attribute) {
                $path[] = sprintf('%s[%s]', $attribute->nodeName, $attribute->nodeValue);
            }
        }
        $path[] = $node->nodeName;
    }
    while ($node = $node->parentNode);

    $result[implode('/', array_reverse($path))] = $val;
}

print_r($result);

Output:

Array
(
    [#document/root/Computer/id[1]/OS/Name] => Linux
    [#document/root/Computer/id[1]/OS/Age] => Older than me
    [#document/root/Computer/id[2]/OS/Name] => Windows
    [#document/root/Computer/id[2]/OS/Age] => Not so much
)

Thats not exactly what you're looking for, but it's a start and can easily be tweaked to give different results.

这篇关于XML转换成使用PHP关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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