我想合并这两个数组[关闭]

I need some help in merging these two arrays.

$array = array(
  'a' => array('b', 'd', 'c'), 
  'b' => array('a', 'e', 'f')
);

I want my output to be like this (merged the two nested arrays above, then sorted)

['a', 'b', 'c', 'd', 'e', 'f']

What I have tried so far is this

foreach($array as $element=>$inner_element)
{
  $length = count($inner_element);
  for($x = 0; $x < $length; $x++)
  {
    echo $inner_element[$x];
    echo "<br>";
  }
}

This shows what the structure looks like, but I have no idea on how to proceed.

$result_array = array_merge($array['a'],$array['b']);

I am hoping you want to merge the arrays inside the main $array variable

Fastest way to merge two arrays:

<?php $newArray = $array1 + $array2; ?>

amaze

As the question appears to be lazy (it is actually harder to create a question here than it is to type the same thing into google), let me provide you with the answer that you wouldn't have found that way easily.

$array = array(
  'a' => array('b', 'c', 'd'),
  'b' => array('a', 'e', 'f')
);
$result = call_user_func_array('array_merge', $array);

This version allows you to merge whatever amount of arrays you have in $array.


The question has been extended with an additional desire to sort the output array. Just add the following to the code above:

sort($result);