将 XML 文档解析为多维数组

发布于 2024-11-04 22:35:50 字数 1596 浏览 0 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2024-11-11 22:35:50

将您的代码更改为此 - 请注意,您的调用 subMenuRecursionArray 返回一个数组,但您没有将其分配给任何东西 - 所以它只是丢失了。

<?php
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++;

            $array["nest$nestcount"]['items'] = 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>';
?>

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.

<?php
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++;

            $array["nest$nestcount"]['items'] = 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>';
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文