任何人都可以帮助将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>
Run Code Online (Sandbox Code Playgroud)
理想情况下应该给:
[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'
Run Code Online (Sandbox Code Playgroud)
但我得到了这个结果:
[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'
Run Code Online (Sandbox Code Playgroud)
这样[Computer-id]键不是唯一的.我正在使用递归函数来读取值,但我无法弄清楚如何将属性名称和属性值转换为从属键的名称...(顺便说一句,有一个很好的理由这样做看似不合逻辑的任务!)任何帮助将不胜感激......
这是在将XML数据读入多维数组后"展平"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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
mik*_*628 42
这对我很有用,而且很简单.
$ob = simplexml_load_file('test.xml');
$json = json_encode($ob);
$array = json_decode($json, true);
Run Code Online (Sandbox Code Playgroud)
一个例子可能是:
$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);
Run Code Online (Sandbox Code Playgroud)
输出:
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
)
Run Code Online (Sandbox Code Playgroud)
这不完全是你想要的,但它是一个开始,可以很容易地调整,以给出不同的结果.
小智 5
这是我生成关联数组的函数,派生自
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);
Run Code Online (Sandbox Code Playgroud)
生成的数组:
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
)
)
)
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17119 次 |
| 最近记录: |