-1

is there a way to loop through an array and unset any variables that are =""? i am looking for a faster way to do this aside form writing 4 if else statements.i thought this might work but i don't know if it can be done this way or not.

$a=""
$b="123"
$c=""
$d"123"

$var=array($a,$b,$c,$d)

i am trying to loop through $var array to get

$var= array($b,$d)

is this even possible or should i stick with writing 4 if else statements?

sstokes
  • 3
  • 1

2 Answers2

0
$x=["","123","","345"];
$var = array_filter($x);
print_r($var);


The result:
Array
(
    [1] => 123
    [3] => 345
)
ArunasR
  • 1,907
  • 1
  • 14
  • 15
0

Have a look here : How to delete object from array inside foreach loop? or here : How do you remove an array element in a foreach loop?

foreach ($array as $key => $value) {
  if($value == "") {
    unset($array[$key]);
  } 
}

Good luck

Community
  • 1
  • 1
Thomas Leduc
  • 1,092
  • 1
  • 20
  • 44