PHP将11个数组合并为一个

时间:2017-04-15 15:29:23

标签: php arrays multidimensional-array merge

我正在尝试将11个不同的数组合并为一个。 这是我的代码到目前为止的样子:

$combine = [];
foreach($data as $key => $array){
    $combine[$key] = array_merge($array,$data1[$key],$data2[$key],$data3[$key],$data4[$key],$data5[$key],$data6[$key],$data7[$key],$data8[$key],$data9[$key],$data10[$key]);
}

以下是一些数组的样子:

$data

array(10) {
[0]=>
  array(2) {
    ["VideoName"]=>
    string(61) "TUTORIAL 1: How to Give an Awesome (PowerPoint) Presentation "
    ["1min"]=>
    string(2) "35"
  }
  [1]=>
  array(2) {
    ["VideoName"]=>
    string(48) "TUTORIAL 2: How to open and close presentations?"
    ["1min"]=>
    string(2) "30"
  }
  [2]=>
  array(2) {
    ["VideoName"]=>
    string(47) "TUTORIAL 3: Make a presentation like Steve Jobs"
    ["1min"]=>
    string(2) "12"
  }
  [3]=>
  array(2) {
    ["VideoName"]=>
    string(56) "TUTORIAL 4: The five secrets of speaking with confidence"
    ["1min"]=>
    string(2) "20"
  }
  [4]=>
  array(2) {
    ["VideoName"]=>
    string(93) "X.EXAMPLE 1 - Abraham Heifets: How can we make better medicines? Computer tools for chemistry"
    ["1min"]=>
    string(2) "24"
  }
  [5]=>
  array(2) {
    ["VideoName"]=>
    string(70) "X.EXAMPLE 1 - Carol Dweck: The power of believing that you can improve"
    ["1min"]=>
    string(1) "2"
  }
  [6]=>
  array(2) {
    ["VideoName"]=>
    string(66) "X.EXAMPLE 2 - Johanna Blakley: Social media and the end of gender "
    ["1min"]=>
    string(2) "15"
  }
  [7]=>
  array(2) {
    ["VideoName"]=>
    string(56) "X.EXAMPLE 3 - Tim Berners-Lee: A Magna Carta for the web"
    ["1min"]=>
    string(2) "17"
  }
  [8]=>
  array(2) {
    ["VideoName"]=>
    string(57) "X.EXAMPLE 4 - Eli Pariser: Beware online "filter bubbles""
    ["1min"]=>
    string(1) "1"
  }
  [9]=>
  array(2) {
    ["VideoName"]=>
    string(103) "X.EXAMPLE 4 - Jasdeep Saggar: Hypoxia-activated pro-drugs: a novel approach for breast cancer treatment"
    ["1min"]=>
    string(2) "21"
  }
}

$data5

array(6) {
  [0]=>
  array(2) {
    ["VideoName"]=>
    string(48) "TUTORIAL 2: How to open and close presentations?"
    ["6min"]=>
    string(2) "24"
  }
  [1]=>
  array(2) {
    ["VideoName"]=>
    string(47) "TUTORIAL 3: Make a presentation like Steve Jobs"
    ["6min"]=>
    string(2) "21"
  }
  [2]=>
  array(2) {
    ["VideoName"]=>
    string(56) "TUTORIAL 4: The five secrets of speaking with confidence"
    ["6min"]=>
    string(2) "16"
  }
  [3]=>
  array(2) {
    ["VideoName"]=>
    string(66) "X.EXAMPLE 2 - Johanna Blakley: Social media and the end of gender "
    ["6min"]=>
    string(2) "10"
  }
  [4]=>
  array(2) {
    ["VideoName"]=>
    string(56) "X.EXAMPLE 3 - Tim Berners-Lee: A Magna Carta for the web"
    ["6min"]=>
    string(2) "11"
  }
  [5]=>
  array(2) {
    ["VideoName"]=>
    string(57) "X.EXAMPLE 4 - Eli Pariser: Beware online "filter bubbles""
    ["6min"]=>
    string(1) "1"
  }
}

$data10

array(1) {
  [0]=>
  array(2) {
    ["VideoName"]=>
    string(70) "X.EXAMPLE 1 - Carol Dweck: The power of believing that you can improve"
    ["11min"]=>
    string(1) "2"
  }
}

但是,当我运行代码时,这就是我得到的thisthis

它完美地合并了第一个视频的所有内容,但之后显示为null。你能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

你可以用这样简单的方式来做到这一点,

$combine = [];

$datas = [];
foreach(range(1, 10) as $index)
{
  $variable = 'data' . $index;
  $datas[] = $$variable;
}

$combine = $data;
foreach($datas as $data){
  foreach($data as $key => $array;
  {
    foreach($array as $k => $v)
    {
      $combine[$key][$k] = $v;
    }
  }
}