0

I'm working in PHP and I need a function that will merge user specified sub-arrays within a 2D array into a new array, e.g.

$arr = [
    "sub_1" => [ "a", "b" ],
    "sub_2" => [ "c", "d" ],
    "sub_3" => [ "e", "f" ],
    "sub_a" => [ 1, 2, 3 ],
    "sub_b" => [ 4, 5, 6 ],
    "sub_c" => [ 7, 8, 9 ]
];

merge_subs( $arr, [ "sub_1", "sub_2", "sub_3" ] );
// should return: [ "a", "b", "c", "d", "e", "f" ]

merge_subs( $arr, [ "sub_a", "sub_b", "sub_c" ] );
// should return: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

What is the most efficient way to go about this?

EDIT:

This is what I tried but it doesn't work so I am clearly doing something wrong.

function merge_subs( $arr, $subs ){
    $new = [];
    foreach( $subs as $sub ){
        array_push( $new, $arr[$sub] );
    }
    return array_merge( $new );
}

EDIT 2:

I realized I left out the ellipsis and now I have it working. At any rate, I want to know if there is a better way to accomplish this.

4
  • Well you show us the way you did it, then we can see if we can make it more efficient Commented Jun 22, 2021 at 16:18
  • 1
    Did you try a simple look through the PHP manual? You may have found array_merge() if you had! Commented Jun 22, 2021 at 16:22
  • I have edited my question to reflect what I have tried. Commented Jun 22, 2021 at 16:39
  • If you need to know which array to process and the names of the sub arrays you want to merge. it would be simpler to do $x = array_merge($arr['sub_1'], $arr['sub_2'], $arr['sub_3']); print_r($x); Commented Jun 22, 2021 at 16:43

2 Answers 2

1

Your looking for $new = array_merge($new, $arr[$sub]);

<?php
$arr = [
    "sub_1" => [ "a", "b" ],
    "sub_2" => [ "c", "d" ],
    "sub_3" => [ "e", "f" ],
    "sub_a" => [ 1, 2, 3 ],
    "sub_b" => [ 4, 5, 6 ],
    "sub_c" => [ 7, 8, 9 ]
];

function merge_subs( $arr, $subs ){
    $new = [];
    foreach( $subs as $sub ){
        $new = array_merge($new, $arr[$sub]);
    }
    return $new;
}


print_r(merge_subs( $arr, [ "sub_1", "sub_2", "sub_3" ] ));
// should return: [ "a", "b", "c", "d", "e", "f" ]

print_r(merge_subs( $arr, [ "sub_a", "sub_b", "sub_c" ] ));

Result:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)
Sign up to request clarification or add additional context in comments.

Comments

1

The accepted answer does what you want by taking an array of keys and looping, but why? Just specify the the keys you want to merge:

$result = array_merge($arr[ "sub_1"], $arr["sub_2"], $arr["sub_3"]);

More involved than looping, but for fun:

function merge_subs($array, $keys) {
    return array_merge(...array_values(array_intersect_key($array, array_flip($keys))));
}
  • Get the elements from $arr with the same $keys passed array_intersect_key
  • Get the values array_values
  • Merge them as you would in the first example unpacking with ...

2 Comments

I specified that I needed a function, so that this can be performed as needed with any provided array and specified subarrays. Can you demonstrate a function that will do what I need and does not use looping?
It's still another working option, always good to have!

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.