In laravel, I have a working code block where I loop an array and within it I call an email list. However, there are certain elements in the array that have a specific email that I want to add to it. So I always want the existing email config list to be used no matter what, but if the array element of the person has an email key I want to add that to the emails array, if that makes sense.
Here is the existing code as it:
$items = array(
array(
"employee" => 123,
"name" => "Tom",
"email" => "[email protected]"
),
array(
"employee" => 234,
"name" => "Sally"
),
array(
"employee" => 345,
"name" => "Rob"
),
array(
"employee" => 456,
"name" => "Ed"
"email" => "[email protected]"
),
array(
"employee" => 567,
"name" => "Barbara"
)
);
foreach($items as $item){
//if there is an override email in the command
if($this->option('email') != 'default') {
if(!filter_var($this->option('email'), FILTER_VALIDATE_EMAIL)) {
$this->error('invalid email:'.$this->option('email'));
return;
}
$emails = ['to'=>[$this->option('email')],'cc'=>[]];
} else {
//This line works as is but I want a condition so that if the array element has and "email" key, I add that value as well
$emails = config('emails.printouts.employeePrintOuts');
//here I would need to add the array value (if email is set in that element)
}
...
}
The part in question is only in the 'else' block. How can I properly do this so that only certain array elements will have an email key and if they do I add it to the emails array that is always using the config list?
array_key_exists()?in_array()function. Try this: ` if (array_key_exist('email',$item)){ if (!in_array($item['email],$emails['to'])){ $emails['to'][] = $item['email']; } } `