0

I'm using a foreach to build this :

array (size=3)
  'trainid' => string '76795' (length=5)
  'traintype' => string ' -X' (length=3)
  'userid' => string 'CPN' (length=3)

array (size=3)
  'trainid' => string '27725' (length=5)
  'traintype' => string ' -Z' (length=3)
  'userid' => string 'CPN' (length=3)

array (size=0)
  empty

array (size=3)
  'trainid' => string '00000' (length=5)
  'traintype' => string ' -X' (length=3)
  'userid' => string 'CPN' (length=3)

array (size=3)
  'trainid' => string '27921' (length=5)
  'traintype' => string ' -Z' (length=3)
  'userid' => string 'CPN' (length=3)

And as you see, there is an empty array and I would like to entirely remove this array. In fact, it crashs my sql script if there is an empty array.

Do you know how to remove it?

Thank you!

2 Answers2

1

You can use array_filter, if no callback is provided, all entries equal to FALSE will be removed.

$array2 = array_filter($array);
Kristian Vitozev
  • 5,791
  • 6
  • 36
  • 56
0

Simply use array_filter(), It will automatically remove empty variable in array..

   print_r(array_filter($arrayvariable));
VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63