I have written IF statement for checking if variables are not empty:
if ( !empty( $firstname )) {
$data = array_merge( $data, array( 'first_name' => $firstname ));
}
if ( !empty( $lastname )) {
$data = array_merge( $data, array( 'last_name' => $lastname ));
}
if ( !empty( $email )) {
$data = array_merge( $data, array( 'email' => $email ));
}
// and there are even more statements then these 3 ...
I think this is so DRY and tried to use SWITCH instead of IF:
switch( true ){
case ( !empty( $firstname )):
$data = array_merge( $data, array( 'first_name' => $firstname ));
case ( !empty( $lastname )):
$data = array_merge( $data, array( 'last_name' => $lastname ));
case ( !empty( $email )):
$data = array_merge( $data, array( 'email' => $email ));
Finally I get an array of all these 3 elements merged even if one of these variables is empty. What do i do wrong?
P.S. I can't do IF(!empty(a) && !empty(b) && !empty(c)) because of need to check each condition separately.
$firstnamean array?compactxarray_filtersolution, like the one I've given below!