0

I have 2 arrays, each will always have the same number of rows and same number of values per row.

I need to merge the 2 arrays together, to combine the results on each row, but in a particular way (there will always be only 3 results per row on each array too):

For example, for each row, take the first result of each array, and put them next to each other, then the second result of each array, and put them next to each other, then finally the third.

So Array 1's value will always precede Array 2's value (example shown below):

Array 1:

array:7 [▼
  24 => array:3 [▼
    0 => 0
    1 => 0.66666666666667
    2 => 0.66666666666667
  ]
  25 => array:3 [▶]
  26 => array:3 [▶]
  27 => array:3 [▶]
  29 => array:3 [▶]
  30 => array:3 [▶]
  31 => array:3 [▶]
]

Array 2:

array:7 [▼
  24 => array:3 [▼
    0 => 0.375
    1 => 0.42857142857143
    2 => 0.55555555555556
  ]
  25 => array:3 [▶]
  26 => array:3 [▶]
  27 => array:3 [▶]
  29 => array:3 [▶]
  30 => array:3 [▶]
  31 => array:3 [▶]
]

Intended Combined Array Format:

array:7 [▼
  24 => array:6 [▼
    0 => 0
    1 => 0.375
    2 => 0.66666666666667
    3 => 0.42857142857143
    4 => 0.66666666666667
    5 => 0.55555555555556
  ]
  25 => array:6 [▶] ...

Current loop which returns the incorrect layout:

$results = array();
foreach ($questionDetails as $key => $question) {
    for ($i = 0; $i < 3; $i++) {
            $results[$key][] = $array1[$key] + $array2[$key];
    }
}

Returns:

array:7 [▼
  24 => array:3 [▼
    0 => array:3 [▼
      0 => 0
      1 => 0.66666666666667
      2 => 0.66666666666667
    ]
    1 => array:3 [▼
      0 => 0
      1 => 0.66666666666667
      2 => 0.66666666666667
    ]
    2 => array:3 [▼
      0 => 0
      1 => 0.66666666666667
      2 => 0.66666666666667
    ]
  ]
  25 => array:3 [▶]
  26 => array:3 [▶]
  27 => array:3 [▶]
  29 => array:3 [▶]
  30 => array:3 [▶]
  31 => array:3 [▶]
]

I'm unsure why my loop isn't just adding the three values from each row together - but then I think they still won't be in the right order, but I'm unsure of how to approach this.

Many thanks.

2 Answers2

0

I would do this:

$combined = array();
foreach($array1 as $key => $value){
    $combined[$key] = array();
    foreach($value as $key2 => $value2){
        $combined[$key][] = $value2;
        $combined[$key][] = $array2[$key][$key2];
    }
}

For a variable number of records.

Agu Dondo
  • 12,638
  • 7
  • 57
  • 68
0

This is the "unfancy" (but save) solution if I understand your question correctly - all keys are preserved an set as desired:

$array1;
$array2;

$results = array();
foreach ($questionDetails as $key => $question1) {
    $question2 = $array2[$key];
    $tp = array();
    $tp[0] = $question1[0];
    $tp[1] = $question2[0];
    $tp[2] = $question1[1];
    $tp[3] = $question2[1];
    $tp[4] = $question1[2];
    $tp[5] = $question2[2];
    $results[$key] = $tp;
}

EDIT: There is a way more flexible way to implement this where the number of arguments may vary (see PHP: array_merge() in alternate order (zip order)?).

    $array1;
    $array2;

    function array_zip(...$arrays) {
        return array_merge(...array_map(null, ...$arrays));
    }

    $results = array();
    foreach ($questionDetails as $key => $question1) {
        $results[$key] = array_zip($question1,$array2[$key]);
    }
Community
  • 1
  • 1
Blackbam
  • 17,496
  • 26
  • 97
  • 150