根据键将 PHP 数组展平一级

发布于 2024-11-19 01:21:39 字数 6660 浏览 6 评论 0原文

我目前有一个输出如下的数组:

Array
(
  [events_response] => Array
    (
      [total_events] => 3
      [0] => Array
        (
          [events_list] => Array
            (
              [0] => Array
                (
                  [event] => Array
                    (
                      [id] => 41
                      [ages] => All
                      [buy_link] =>
                      [created] => 2011-06-06 22:13:02
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 7
                              [name] => Metrodome
                              [website] =>
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => 123 Any Street
                                      [city] => My City
                                      [state_province] =>
                                      [postal_code] => 12345
                                      [country] =>
                                    )
                                )
                            )
                        )
                      [1] => Array
                        (
                          [collaborators_list] => Array
                            (
                              [0] => Array
                                (
                                  [collaborator] => Array
                                    (
                                      [id] => 3
                                      [name] => derp
                                      [website] => http://derp.com/
                                    )
                                )
                              [1] => Array
                                (
                                  [collaborator] => Array
                                    (
                                      [id] => 4
                                      [name] => "Foo" Bar
                                      [website] => http://www.foobar.com/
                                    )
                                )
                            )
                        )
                      [2] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
              [1] => Array
                (
                  [event] => Array
                    (
                      [id] => 64
                      [ages] => 21+
                      [buy_link] => dsfdsaf
                      [created] => 2011-07-05 21:35:52
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 8
                              [name] => name
                              [website] => website.com
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => street address
                                      [city] => city
                                      [state_province] => WI
                                      [postal_code] => 53103
                                      [country] => USA
                                    )
                                )
                            )
                        )
                      [collaborators_list] =>
                      [1] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
              [2] => Array
                (
                  [event] => Array
                    (
                      [id] => 65
                      [ages] => 21+
                      [buy_link] => dsfdsaf
                      [created] => 2011-07-05 21:36:12
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 8
                              [name] => name
                              [website] => website.com
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => street address
                                      [city] => city
                                      [state_province] => WI
                                      [postal_code] => 53103
                                      [country] => USA
                                    )
                                )
                            )
                        )
                      [collaborators_list] =>
                      [1] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
            )
        )
    )
)

我需要做的基本上是删除其中有一个 int 键的级别。因此,例如,[total_events] 将与 [events_list] 处于同一级别,而中间没有 [0]。对于其他有 [] 键的区域也是如此。

我正在使用 PHP,并尝试了各种尝试来扁平化它,但还没有完全实现。

提前致谢!

I currently have an array that's outputting as follows:

Array
(
  [events_response] => Array
    (
      [total_events] => 3
      [0] => Array
        (
          [events_list] => Array
            (
              [0] => Array
                (
                  [event] => Array
                    (
                      [id] => 41
                      [ages] => All
                      [buy_link] =>
                      [created] => 2011-06-06 22:13:02
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 7
                              [name] => Metrodome
                              [website] =>
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => 123 Any Street
                                      [city] => My City
                                      [state_province] =>
                                      [postal_code] => 12345
                                      [country] =>
                                    )
                                )
                            )
                        )
                      [1] => Array
                        (
                          [collaborators_list] => Array
                            (
                              [0] => Array
                                (
                                  [collaborator] => Array
                                    (
                                      [id] => 3
                                      [name] => derp
                                      [website] => http://derp.com/
                                    )
                                )
                              [1] => Array
                                (
                                  [collaborator] => Array
                                    (
                                      [id] => 4
                                      [name] => "Foo" Bar
                                      [website] => http://www.foobar.com/
                                    )
                                )
                            )
                        )
                      [2] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
              [1] => Array
                (
                  [event] => Array
                    (
                      [id] => 64
                      [ages] => 21+
                      [buy_link] => dsfdsaf
                      [created] => 2011-07-05 21:35:52
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 8
                              [name] => name
                              [website] => website.com
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => street address
                                      [city] => city
                                      [state_province] => WI
                                      [postal_code] => 53103
                                      [country] => USA
                                    )
                                )
                            )
                        )
                      [collaborators_list] =>
                      [1] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
              [2] => Array
                (
                  [event] => Array
                    (
                      [id] => 65
                      [ages] => 21+
                      [buy_link] => dsfdsaf
                      [created] => 2011-07-05 21:36:12
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 8
                              [name] => name
                              [website] => website.com
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => street address
                                      [city] => city
                                      [state_province] => WI
                                      [postal_code] => 53103
                                      [country] => USA
                                    )
                                )
                            )
                        )
                      [collaborators_list] =>
                      [1] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
            )
        )
    )
)

What I need to do is basically remove the levels where there is a key that is an int. So, for instance, [total_events] would be on the same level as [events_list] without that [0] in between. The same goes for other areas where there is a [<int>] key.

I'm using PHP and have tried various attempts at flattening it but haven't been able to quite get there.

Thanks in advance!

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

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

发布评论

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

评论(2

红玫瑰 2024-11-26 01:21:39
function flattenByRemoveingIntKeys($array)
{
    foreach ($array as $key => $val)
    {
        if (is_array($val))
            $array[$key] = flattenByRemoveingIntKeys($val);
        else if (is_numeric($key))
        {
            $array .= $val[$key];
            unset($array[$key]);
        }
    }
}

没有真正测试过,但类似的东西应该有效。

function flattenByRemoveingIntKeys($array)
{
    foreach ($array as $key => $val)
    {
        if (is_array($val))
            $array[$key] = flattenByRemoveingIntKeys($val);
        else if (is_numeric($key))
        {
            $array .= $val[$key];
            unset($array[$key]);
        }
    }
}

Not really tested but something like that should work.

陌若浮生 2024-11-26 01:21:39

试试这个:

function RemoverIntKeys($array) { 
   foreach($array as $key => $value) {
        if(preg_match("/^\d+$/",$key)) {
         unset($array[$key]); 
       }
  }
           return $array;
}

$array = array("foo" => "baa", 2 => "hehe", "test" => "..", 4 => "hello"); 
$newarray = RemoverIntKeys($array); 
echo "<pre>";
print_r($newarray); 

输出:

 Array
    (
        [foo] => baa
        [test] => ..
    )

Try this:

function RemoverIntKeys($array) { 
   foreach($array as $key => $value) {
        if(preg_match("/^\d+$/",$key)) {
         unset($array[$key]); 
       }
  }
           return $array;
}

$array = array("foo" => "baa", 2 => "hehe", "test" => "..", 4 => "hello"); 
$newarray = RemoverIntKeys($array); 
echo "<pre>";
print_r($newarray); 

Output:

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