数组到多维数组...基于数组键名中的 foo.bar.baz-dots

发布于 2024-11-19 10:23:35 字数 323 浏览 1 评论 0原文

我有一个数组,其中“foo.bar.baz”作为数组中的键名。有没有一种方便的方法可以将此数组转换为多维数组(使用每个“点级别”作为下一个数组的键)?

  • 实际输出: Array([foo.bar.baz] => 1, [qux] => 1)
  • 期望输出: Array([foo][bar][baz] => 1, [qux] => 1 1)

代码示例:

$arr = array("foo.bar.baz" => 1, "qux" => 1);
print_r($arr);

I have an array with "foo.bar.baz" as key names in the array. Is there a handy way to turn this array into a multidimensional array (using each "dot level" as key for the next array)?

  • Actual output: Array([foo.bar.baz] => 1, [qux] => 1)
  • Desired output: Array([foo][bar][baz] => 1, [qux] => 1)

Code example:

$arr = array("foo.bar.baz" => 1, "qux" => 1);
print_r($arr);

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

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

发布评论

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

评论(1

十年九夏 2024-11-26 10:23:35

解决方案:

<?php

$arr = array('foo.bar.baz' => 1, 'qux' => 1);

function array_dotkey(array $arr)
{
  // Loop through each key/value pairs.
  foreach ( $arr as $key => $value )
  {
    if ( strpos($key, '.') !== FALSE )
    {
      // Reference to the array.
      $tmparr =& $arr;

      // Split the key by "." and loop through each value.
      foreach ( explode('.', $key) as $tmpkey )
      {
        // Add it to the array.
        $tmparr[$tmpkey] = array();

        // So that we can recursively continue adding values, change $tmparr to a reference of the most recent key we've added.
        $tmparr =& $tmparr[$tmpkey];
      }

      // Set the value.
      $tmparr = $value;

      // Remove the key that contains "." characters now that we've added the multi-dimensional version.
      unset($arr[$key]);
    }
  }

  return $arr;
}

$arr = array_dotkey($arr);
print_r($arr);

输出:

Array
(
    [qux] => 1
    [foo] => Array
        (
            [bar] => Array
                (
                    [baz] => 1
                )

        )

)

Solution:

<?php

$arr = array('foo.bar.baz' => 1, 'qux' => 1);

function array_dotkey(array $arr)
{
  // Loop through each key/value pairs.
  foreach ( $arr as $key => $value )
  {
    if ( strpos($key, '.') !== FALSE )
    {
      // Reference to the array.
      $tmparr =& $arr;

      // Split the key by "." and loop through each value.
      foreach ( explode('.', $key) as $tmpkey )
      {
        // Add it to the array.
        $tmparr[$tmpkey] = array();

        // So that we can recursively continue adding values, change $tmparr to a reference of the most recent key we've added.
        $tmparr =& $tmparr[$tmpkey];
      }

      // Set the value.
      $tmparr = $value;

      // Remove the key that contains "." characters now that we've added the multi-dimensional version.
      unset($arr[$key]);
    }
  }

  return $arr;
}

$arr = array_dotkey($arr);
print_r($arr);

Outputs:

Array
(
    [qux] => 1
    [foo] => Array
        (
            [bar] => Array
                (
                    [baz] => 1
                )

        )

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