0

So I have 2 arrays like so:

$a = array(array('1','2','3'), array('hello','darkness','my'));
$b = array(array('4','5','6'), array('old','friend','I'));

Now I want to merge the subarrays respectively:

$result = array(array_merge($a[0],$b[0]),array_merge($a[1],$b[1])));

Resulting in:

#vardump($result)
array(2) {
  [0]=>
  array(6) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "4"
    [4]=>
    string(1) "5"
    [5]=>
    string(1) "6"
  }
  [1]=>
  array(6) {
    [0]=>
    string(5) "hello"
    [1]=>
    string(8) "darkness"
    [2]=>
    string(2) "my"
    [3]=>
    string(3) "old"
    [4]=>
    string(6) "friend"
    [5]=>
    string(1) "I"
  }
}

Well this works... but it just looks clumsy and it isn't a good solution if there would be a variable amount of subarrays I need to merge. Is there a build-in function for this kind of operation (or a more "acceptable" way to do this)?

3
  • You can just use a Foreach to do it assuming both arrays are formatted alike. Commented Jun 10, 2019 at 22:58
  • @Forbs yeah I thought so, but I also don't really like the look of loops ;/ Commented Jun 10, 2019 at 23:00
  • Loops are LIFE in php..haha...they grow and shrink depends on what you need. Commented Jun 10, 2019 at 23:09

2 Answers 2

1

I came up with the following (with the help of @panther's answer)

function subarray_merge(...$arrays){
    $result = array();
    foreach ($arrays as $k => $v) {
        if($k === 0) {
            $result = $v;
        }else{
            foreach ($v as $ks => $vs) {
                $result[$ks] = array_merge($result[$ks], $v[$ks]);
            }
        }
    }
    return $result;
}

print_r(subarray_merge($a,$b,$c));
Sign up to request clarification or add additional context in comments.

1 Comment

It's the second correct solution near wrapping array. Glad to help you!
0

Just use a foreach cycle.

<?php

$a = array(array('1','2','3'), array('hello','darkness','my'));
$b = array(array('4','5','6'), array('old','friend','I'));
$c = array(array('7','8','9'), array('old','friend','I'));

$defined_vars = get_defined_vars();

$result = [];

foreach ($defined_vars as $key => $arr) {
    if (in_array($key, ['_GET', '_POST', '_COOKIE', '_FILES', '_SESSION'])) continue;

    foreach ($arr as $k => $row) {
        if (!isset($result[$k])) {
            $result[$k] = $row;
        } else {
            foreach ($row as $r) {
                $result[$k][] = $r;
            }
        }
    }
}

echo '<pre>';
print_r($result);


--- result
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
        )

    [1] => Array
        (
            [0] => hello
            [1] => darkness
            [2] => my
            [3] => old
            [4] => friend
            [5] => I
            [6] => old
            [7] => friend
            [8] => I
        )

)

This solution is 'hell', but working. Correct is to make wrapping array instead these variables, like

$array = [
    array(array('1','2','3'), array('hello','darkness','my')),
    array(array('4','5','6'), array('old','friend','I')),
    array(array('7','8','9'), array('old','friend','I'))
];

$result = [];

foreach ($array as $key => $arr) {
    foreach ($arr as $k => $row) {
        if (!isset($result[$k])) {
            $result[$k] = $row;
        } else {
            foreach ($row as $r) {
                $result[$k][] = $r;
            }
        }
    }
}

echo '<pre>';
print_r($result);

3 Comments

maybe I didn't clarify the question correctly, but I meant if I'd add more "subarrays", so for example adding another like this $c = array(array('7','8','9'), array('ve','come','to'));
@YTZ: ah, okay. Than you need to use get_defined_vars() and loop over that. But the right one solution is to make another array wrapping these vars.
Okay cool. I'll accept your answer and I'll also post my own answer, where I used your pre-edited answer to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.