简体   繁体   English

如何合并这个数组的PHP

[英]how to merge this array php

This is my first array var_dump 这是我的第一个数组var_dump

array(1) {
  [0]=>
  array(55) {
    ["Primary Maths"]=>
    NULL
    ["Primary Environment"]=>
    NULL
    ["Primary English"]=>
    NULL
    ["Primary Sinhala"]=>..........etc

In first array contain 55 array elements. 在第一个数组中包含55个数组元素。 .

This is my second array var_dump 这是我的第二个数组var_dump

array(1) {
  [0]=>
  array(660) {
    ["Primary_MathsJAN"]=>
    string(7) "checked"
    ["Primary_MathsFEB"]=>
    string(7) "checked"
    ["Primary_MathsMAR"]=>
    string(7) "checked" ...etc

In first array contain 660 array elements. 在第一个数组中包含660个数组元素。 .

So i want to merge(JOIN) first array with second array.But i want it like this.I want to join first array one value with second array 12 values... 所以我想将第一个数组与第二个数组合并(JOIN)。但是我想要这样。我想将第一个数组的一个值与第二个数组的12个值连接起来...

1st array elements(55) * 12 = 2nd array elements(660)

Is there anyway to do this ? 反正有这样做吗?

There might be some combination of built in functions you could use, but every time I find myself in a situation like this I'd rather just write 2 loops and be done with it. 您可能会使用内置函数的某种组合,但是每次我遇到这种情况时,我宁愿只编写2个循环并完成它。 In case you decide to follow my example what you can do is foreach the first array and then use a regular for with 12 loops for the second array within the foreach. 如果您决定按照我的示例操作,可以对第一个数组进行foreach,然后对foreach中的第二个数组使用带有12个循环的常规for。 You might also want to use array_values on the second array in order to save yourself the trouble of fighting keys. 您可能还想在第二个数组上使用array_values,以免自己与密钥打交道。

Best wishes and good luck! 祝你好运,祝你好运!

This will split the 2nd array into an multidimentional array into 12 element chunks, and combine it with the first array. 这会将第二个数组拆分为12个元素块的多维数组,并将其与第一个数组组合。 However, it looks like you need to use the keys from the 1st array, so I added the function array_keys: 但是,您似乎需要使用第一个数组中的键,因此我添加了array_keys函数:

array_combine(array_keys($a1), array_chunk($a2, 12))

If I have well understand the question, this will do the job: 如果我很好地理解了这个问题,那么就可以完成此工作:

$result = $array1[0];
$temp = array_chunk($array2[0],12);
foreach ($result as &$value) {
    $value = array_shift($temp)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM