Reputation: 23
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.
Upvotes: 0
Views: 901
Reputation: 656
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;
Upvotes: 0
Reputation: 7485
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]',
)
Upvotes: 0
Reputation: 897
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
Upvotes: 0
Reputation: 1367
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
Upvotes: 3