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

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

问题描述

任何人都可以帮助将数据从 XML 文档转换为关联数组吗?我遇到了一些问题,因为 XML 结构是一种 3D 结构,而数组更像是一种 2D 结构(请原谅我缺乏正确的术语).XML 元素有属性、子元素和孙子元素(但我从来不知道它们的名字),所以我想我会尝试将数组中的键连接成每个子元素/属性名称和值等于,好吧,价值.问题是我需要属性名称和值作为连接数组键的一部分以使其唯一...

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...

例如:

<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>

理想情况下应该给出:

[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'

但我得到了这个结果:

[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'

这样 [Computer-id] 键就不是唯一的.我正在使用递归函数来读入值,但我无法弄清楚如何将属性名称和属性值放入从属键的名称中......(顺便说一句,这样做是有充分理由的看似不合逻辑的任务!)任何帮助将不胜感激...

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...

这是将 XML 数据读入多维数组后扁平化"的函数.我不确定我是否以正确的方式解决这个问题!

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";
        }
    }
}

推荐答案

一个例子可能是:

$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);

输出:

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.

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

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