根据键将 PHP 数组展平一级
我目前有一个输出如下的数组:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有真正测试过,但类似的东西应该有效。
Not really tested but something like that should work.
试试这个:
输出:
Try this:
Output: