将 XML 文档解析为多维数组
我正在尝试从 XML 文件创建无限深度的多维数组。我就不解释原因了,直接进入正题。
我希望下面的代码能够解析 XML 字符串,将所有
和
标签插入到数组中。直接在
标签下找到的任何
标签都应插入到主数组内包含的数组中。
在递归调用中,我已将密钥作为第二个参数传递给新数组,因此我希望它开始向这个新数组添加项目。然而事实并非如此。如果有人可以帮助我解决问题,我将不胜感激。
该代码应使用简单的 C+P 运行。谢谢。
class Parser
{
static function subMenuRecursionArray($xml, $array = '', $itemcount = 1, $nestcount = 1)
{
foreach($xml->children() as $k => $v)
{
if ((string) $k == 'item')
{
$array["item$itemcount"]['text'] = (string) $v;
$array["item$itemcount"]['command'] = (string) $v['command'];
$itemcount++;
}
if ((string) $k == 'nest')
{
$array["nest$nestcount"] = array('name' => (string) $v['name'], 'items' => array());
$nestcount++;
self::subMenuRecursionArray($xml->nest, $array["nest".($nestcount-1)]['items'], $itemcount, $nestcount);
}
}
return $array;
}
}
$xml_fragment = '
<menu>
<item command="DefaultCommand">Main (Not nested 1)</item>
<item command="DefaultCommand">Main 2 (Not nested 2)</item>
<nest name="Cont">
<item command="Contact">NESTED Contact 1</item>
<item command="Contact">NESTED Contact 2</item>
</nest>
</menu>';
$xml = simplexml_load_string($xml_fragment);
$array = Parser::subMenuRecursionArray($xml);
echo '<pre>' . print_r($array, 1) . '</pre>';
I am attempting to create a multidimensional array of unlimited depth from an XML file. I shall spare you the reasons why and just get straight to the point.
I would like the below code to parse an XML string, inserting all <item>
and <nest>
tags into an array. Any <item>
tags that are found directly under a <nest>
tag should be be inserted into an array contained inside the main array.
On the recurisve call I have passed the key to a new array as the second parameter, so I would expect it to begin adding items to this new array. This is not so, however. If anybody could assist me in fixing the problem, I would appreciate it.
The code should run with a simple C+P. Thank you.
class Parser
{
static function subMenuRecursionArray($xml, $array = '', $itemcount = 1, $nestcount = 1)
{
foreach($xml->children() as $k => $v)
{
if ((string) $k == 'item')
{
$array["item$itemcount"]['text'] = (string) $v;
$array["item$itemcount"]['command'] = (string) $v['command'];
$itemcount++;
}
if ((string) $k == 'nest')
{
$array["nest$nestcount"] = array('name' => (string) $v['name'], 'items' => array());
$nestcount++;
self::subMenuRecursionArray($xml->nest, $array["nest".($nestcount-1)]['items'], $itemcount, $nestcount);
}
}
return $array;
}
}
$xml_fragment = '
<menu>
<item command="DefaultCommand">Main (Not nested 1)</item>
<item command="DefaultCommand">Main 2 (Not nested 2)</item>
<nest name="Cont">
<item command="Contact">NESTED Contact 1</item>
<item command="Contact">NESTED Contact 2</item>
</nest>
</menu>';
$xml = simplexml_load_string($xml_fragment);
$array = Parser::subMenuRecursionArray($xml);
echo '<pre>' . print_r($array, 1) . '</pre>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的代码更改为此 - 请注意,您的调用 subMenuRecursionArray 返回一个数组,但您没有将其分配给任何东西 - 所以它只是丢失了。
Change your code to this - notice that your call too subMenuRecursionArray is returning an array, but you aren't assigning that to anything - so it is just lost.