有人可以帮助将XML文档中的数据转换为关联数组吗?我遇到了一些问题,因为XML结构是一种3D结构,而数组更像是2D结构(请原谅我从头到尾都没有正确的术语)。XML元素有属性、子元素和孙元素(但我从来不知道它们的名称),所以我认为可以尝试使数组中的键由每个子元素/属性名和等于值的值串联而成。问题是,我需要将属性名和值作为连接的数组键的一部分,以使其唯一...
例如:
<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'从而计算机id密钥不是唯一的。我使用了一个递归函数来读取值,但是我不知道如何将属性名和属性值放入从属键的名称中……(顺便说一句,做这个看似不合逻辑的任务有一个很好的理由!)任何帮助都将不胜感激。
下面的函数在将XML数据读入多维数组后将其“展平”。我不确定我这样做是不是正确的!
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";
        }
    }
}发布于 2011-06-30 19:25:55
一个例子可能是:
$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
)这不完全是你想要的,但它是一个开始,可以很容易地调整,以得到不同的结果。
发布于 2011-06-30 19:48:21
这对我来说很有效,而且很简单。
$ob = simplexml_load_file('test.xml');
$json = json_encode($ob);
$array = json_decode($json, true);发布于 2014-01-07 18:56:13
下面是生成关联数组的函数,派生自
Recursive cast from SimpleXMLObject to Array
function xml2assoc($obj, &$arr) {
  $children = $obj->children();
  foreach ( $children as $elementName => $node ) {
    if (!isset($arr[$elementName])) {
      $arr[$elementName] = array();
    }
    $temp = array();
    $attributes = $node->attributes();
    foreach ( $attributes as $attributeName => $attributeValue ) {
      $attribName = strtolower(trim((string) $attributeName));
      $attribVal = trim((string) $attributeValue);
      $temp[$attribName] = $attribVal;
    }
    $text = (string) $node;
    $text = trim($text);
    if (strlen($text) > 0) {
      $temp ['text='] = $text;
    }
    $arr[$elementName][] = $temp;
    $nextIdx = count($arr[$elementName]);
    xml2assoc($node, $arr[$elementName][$nextIdx - 1]);
  }
  return;
}
$xml = '<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[title1]]></Title> 
<Description><![CDATA[description1]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[title]]></Title>
<Description><![CDATA[description]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
</xml> ';
$dom = new SimpleXMLElement($xml);
$arr = array();
xml2assoc($dom, $arr);
print_r($arr);生成的数组:
Array
(
    [ToUserName] => Array
        (
            [0] => Array
                (
                    [text=] => toUser
                )
        )
    [FromUserName] => Array
        (
            [0] => Array
                (
                    [text=] => fromUser
                )
        )
    [CreateTime] => Array
        (
            [0] => Array
                (
                    [text=] => 12345678
                )
        )
    [MsgType] => Array
        (
            [0] => Array
                (
                    [text=] => news
                )
        )
    [ArticleCount] => Array
        (
            [0] => Array
                (
                    [text=] => 2
                )
        )
    [Articles] => Array
        (
            [0] => Array
                (
                    [item] => Array
                        (
                            [0] => Array
                                (
                                    [Title] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => title1
                                                )
                                        )
                                    [Description] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => description1
                                                )
                                        )
                                    [PicUrl] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => picurl
                                                )
                                        )
                                    [Url] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => url
                                                )
                                        )
                                )
                            [1] => Array
                                (
                                    [Title] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => title
                                                )
                                        )
                                    [Description] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => description
                                                )
                                        )
                                    [PicUrl] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => picurl
                                                )
                                        )
                                    [Url] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => url
                                                )
                                        )
                                )
                        )
                )
        )
)https://stackoverflow.com/questions/6533790
复制相似问题