将二维数组转换成一维PHP [英] turn 2d array into 1d PHP

查看:122
本文介绍了将二维数组转换成一维PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3阵列,非常类似内容的数组(下面的示例)。我在想,如果有把一切都成一个阵列,而不是3个独立的人一个简单的方法。一切我尝试似乎只是简单地覆盖数据,我留下了只从第3阵列的信息。

I have an array (sample below) containing 3 arrays with very similar content. I was wondering if there was an easy way to put everything into one array, rather than 3 separate ones. Everything I try seems to just overwrite the data and I'm left with the info from the 3rd array only.

array
  0 => 
    array
      'america' => int 19
      'music' => int 6
      'states' => int 5
      'bank' => int 5
  1 => 
    array
      'america' => int 19
      'home' => int 3
      'society' => int 2
      'writers' => int 2
   2 => 
    array
      'america' => int 19
      'lutheran' => int 4
      'church' => int 4
      'national' => int 4
      'cruises' => int 3

理想情况下,最终的结果会是这样的:

Ideally the end result will look like this:

array
          'america' => int 19
          'music' => int 6
          'states' => int 5
          'bank' => int 5
          'america' => int 19
          'home' => int 3
          'society' => int 2
          'writers' => int 2
          'america' => int 19
          'lutheran' => int 4
          'church' => int 4
          'national' => int 4
          'cruises' => int 3

任何解决方案?谢谢

Any solutions? thanks

推荐答案

您将不能够有相同的键值(即美国等3个按键)的关联数组。当您尝试设置这些密钥数组中,你可以简单的覆盖previous值。

You won't be able to have an associative array with the same key values (i.e. 3 keys of "America", etc.). When you try to set those keys in the array, you will simply overwrite the previous value.

这篇关于将二维数组转换成一维PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆