从简单的 xml 对象获取多维数组时出现问题

发布于 2024-09-02 14:14:46 字数 648 浏览 8 评论 0原文

作为新手,我需要您的帮助来从 simplexml 对象获取多维数组。 假设我的数组是这样的: 只是为了获得想法:
这里 $data 是一个 simplexml 对象,包含以下 xml:

<users>
  <user>
    <id></id>
    <nm></nm>
    <gender>
      <male></male>
      <female></female>
    </gender>
  </user>
</users>

这里我完美地获取了每个用户的数组,但是在该数组中,当涉及到性别时,它什么也没有显示。我也需要性别数组。我正在使用以下代码:

foreach($data->users as $users)
{
  $arr1 = array();
  foreach($users as $user)
  {
    foreach($user as $k=>$v)
    {
      $arr1[$k] = (string) $v;
    }
  }
  $arr2[] = $arr1;
}

有什么建议吗?

As a newbie I need your help in getting multidimensional array from simplexml object.
suppose my array is like:
just for getting idea:
Here $data is a simplexml object contains below xml:

<users>
  <user>
    <id></id>
    <nm></nm>
    <gender>
      <male></male>
      <female></female>
    </gender>
  </user>
</users>

Here I an perfectly getting array of each user, but in that array when it comes to gender, it shows nothing. i need array of gender too. I am using following code:

foreach($data->users as $users)
{
  $arr1 = array();
  foreach($users as $user)
  {
    foreach($user as $k=>$v)
    {
      $arr1[$k] = (string) $v;
    }
  }
  $arr2[] = $arr1;
}

Any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

脸赞 2024-09-09 14:14:46

您没有正确使用 foreach 迭代器。它应该是这样的:

$users = simplexml_load_string($the_xml_thing_you_posted);

foreach ($users->user as $user)
{
    $arr[] = array(
        'id' => (string) $user->id,
        'nm' => (string) $user->nm
    );
}

现在我不知道你想对你的性别做什么,这两个元素都是空的,这是什么意思?

You're not using the foreach iterator correctly. It should be something like this:

$users = simplexml_load_string($the_xml_thing_you_posted);

foreach ($users->user as $user)
{
    $arr[] = array(
        'id' => (string) $user->id,
        'nm' => (string) $user->nm
    );
}

Now I have no idea what you're trying to do with your gender thing, both elements are empty, what does that mean?

木格 2024-09-09 14:14:46

foreach($user as $k=>$v) 不会迭代 user 元素中的所有子元素。它迭代作为 $users 子级的所有 user 元素。例如:

$data = new SimpleXMLElement('<data>
  <users id="1">
    <user id="user1a"></user>
    <user id="user1b"></user>
  </users>
  <users id="2">
    <user id="user2a"></user>
    <user id="user2b"></user>
  </users>
</data>');

foreach($data->users as $users)
{
  echo 'users id: ', $users['id'], "\n";
  foreach($users as $user)
  {
    echo "  user id: ", $user['id'], "\n";
  }
}

prints

users id: 1
  user id: user1a
  user id: user1b
users id: 2
  user id: user2a
  user id: user2b

您可能想要更像

$data = new SimpleXMLElement('<data>
  <users>
    <user>
      <propA>A1</propA>
      <propB>B1</propB>
      <propC>C1</propC>
    </user>
    <user>
      <propA>A2</propA>
      <propB>B2</propB>
      <propC>C2</propC>
    </user>
  </users>
</data>');

$result = array();
foreach($data->users as $users)
{
  foreach($users as $user)
  {
    $arr = array();
    foreach($user->children() as $e) {
      $arr[$e->getName()] = (string)$e;
    }
    $result[] = $arr;
  }
}

print_r($result);

which prints 的东西

Array
(
    [0] => Array
        (
            [propA] => A1
            [propB] => B1
            [propC] => C1
        )

    [1] => Array
        (
            [propA] => A2
            [propB] => B2
            [propC] => C2
        )

)

foreach($user as $k=>$v) doesn't iterate over all the child elements in your user element. It iterates over all user elements that are children of $users. For illustration:

$data = new SimpleXMLElement('<data>
  <users id="1">
    <user id="user1a"></user>
    <user id="user1b"></user>
  </users>
  <users id="2">
    <user id="user2a"></user>
    <user id="user2b"></user>
  </users>
</data>');

foreach($data->users as $users)
{
  echo 'users id: ', $users['id'], "\n";
  foreach($users as $user)
  {
    echo "  user id: ", $user['id'], "\n";
  }
}

prints

users id: 1
  user id: user1a
  user id: user1b
users id: 2
  user id: user2a
  user id: user2b

You probably want something more like

$data = new SimpleXMLElement('<data>
  <users>
    <user>
      <propA>A1</propA>
      <propB>B1</propB>
      <propC>C1</propC>
    </user>
    <user>
      <propA>A2</propA>
      <propB>B2</propB>
      <propC>C2</propC>
    </user>
  </users>
</data>');

$result = array();
foreach($data->users as $users)
{
  foreach($users as $user)
  {
    $arr = array();
    foreach($user->children() as $e) {
      $arr[$e->getName()] = (string)$e;
    }
    $result[] = $arr;
  }
}

print_r($result);

which prints

Array
(
    [0] => Array
        (
            [propA] => A1
            [propB] => B1
            [propC] => C1
        )

    [1] => Array
        (
            [propA] => A2
            [propB] => B2
            [propC] => C2
        )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文