Search code examples
phparraysloopsforeachunset

how to loop though an array and unset variables in the array


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?


Solution

  • 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