-1

Is it possible to do this in PHP:

$a['foo'] = [1,2,3];
$a['bar'] = [4,5,6];

Instead of array_merge($a['foo'], $a['bar']), you would use simply use something like array_merge($a) instead?

6
  • 3
    Possible duplicate of How to merge subarray in PHP most easily? Commented Feb 4, 2016 at 16:52
  • $a['foo]' is it intentional or is it a typo? Commented Feb 4, 2016 at 16:52
  • you can write a function for that. is it always foo and bar? Commented Feb 4, 2016 at 16:52
  • Also possible duplicate of stackoverflow.com/questions/17041278/… ? Commented Feb 4, 2016 at 16:53
  • @Nordenheim typo thanks, fixed. Commented Feb 4, 2016 at 16:53

1 Answer 1

1

Live demo

function my_array_merge($array) {
   $result = [];
   foreach($array as $subArray) {
      $result = array_merge($result, $subArray);
   }
   return $result;
} 
Sign up to request clarification or add additional context in comments.

Comments

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.