0

I need to create an array of arrays.
I have been using array_map(null,$a,$b,$c) to do this and it works fine, however, it doesn't work if one of the mapped arrays doesn't exist.

To get around this problem I have used:

$myArray= array();
if (isset($a)) {
    array_push($myArray,$a);
}
if (isset($b)) {
    array_push($myArray,$b);
}
if (isset($c)) {
    array_push($myArray,$c);
}

Is there a more elegant/shorter method of writing this?
I've tried applying some functions via array_map($function,$a,$b,$c) but with no luck.

ticallian
  • 1,631
  • 7
  • 24
  • 34

2 Answers2

4
$myArray = array_filter(array($a, $b, $c));
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

You could use the following function:

function joinArrays(){
  $arrays = func_get_args();
  $output = array();
  foreach($arrays as $array){
     if(!empty($array)) array_push($output, $array);
  }
  return $output;
}

call like: joinArrays($a, $b, $c, etc..);

Pim Jager
  • 31,965
  • 17
  • 72
  • 98