0

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.

8
  • Is $firstname an array? Commented Oct 24, 2020 at 14:04
  • 1
    Far as I know, all cases require breaks, per the manual php.net/manual/en/control-structures.switch.php Commented Oct 24, 2020 at 14:04
  • 1
    also, moving all those checks into a switch statement is no DRYer than the ifs, you're still writing them all out. DRY is for Don't Repeat Yourself, this means don't do the same if statement twice, not don't have lots of if statements Commented Oct 24, 2020 at 14:12
  • 1
    @Domaru that's the point of a switch statement Commented Oct 24, 2020 at 14:13
  • 1
    You could try a simple compact x array_filter solution, like the one I've given below! Commented Oct 24, 2020 at 14:13

4 Answers 4

3

An easy solution would be

$data = array_merge($data, array_filter(compact("firstname", "lastname", "email")));

Compact is a handy function that converts a listing of variable names into an array of its values. Array filter would remove all the empty elements, and lastly you can merge it with $data

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Very clever solution :)
0

Just out of interest I tested your code as I was curious about the comment you make at the end to say that even if a variable is empty then it is still included in the switch. I tested it out

    $firstname = '';//also tested with $firstname = null;
    $lastname = 'joe';
    $email = '[email protected]';
    $data = [];
    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 ));
    }
    print_r($data);

The output is:

Array ( [last_name] => joe [email] => [email protected] )

Which is what I would have expected - I'm only adding this as I think you might have variables you think are empty but aren't. So before implementing the solution already posted, make sure you also check your variables are in the state you think they are

Comments

0

You could just loop and test with not empty by using variable variables.

<?php
$firstname = 'Paul';
$email     = '[email protected]';

$data = ['age' => '34'];
foreach(['firstname', 'lastname', 'email'] as $name) {
    if(!empty($$name)) {
        $data[$name] = $$name;
    }
}

var_export($data);

Output:

array (
'age' => '34',
'firstname' => 'Paul',
'email' => '[email protected]',
)

Comments

0

Using compact function with array_filter was a good enough approach. I'm just sharing an alternative of using ternary operator for your problem's solution.

$data = !empty($firstname) ? array_merge( $data , [ 'firstname' => $firstname] ) : $data;
$data = !empty($lastname)  ? array_merge( $data , [ 'lastname' => $lastname]  ) : $data;
$data = !empty($email)  ? array_merge( $data , [ 'email' => $email] ) : $data;

 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.