0

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?

4
  • array_key_exists()? Commented Aug 10, 2020 at 18:20
  • So if I use that, I could add that email into the array, but then if run the existing line for the config will it add to the array or just replace it? Commented Aug 10, 2020 at 18:32
  • You can use that to check if a key exists in an array, you'll have to write the logic yourself Commented Aug 10, 2020 at 18:35
  • 1
    If you want to not overwrite, but just add email, then you also can check email existing in array with in_array() function. Try this: ` if (array_key_exist('email',$item)){ if (!in_array($item['email],$emails['to'])){ $emails['to'][] = $item['email']; } } ` Commented Aug 10, 2020 at 18:40

1 Answer 1

1

As mentioned in comments, you can use array_key_exist() function to check that $items contain email key.

Also to fullfil $emails['to'] subarray only in case that it doesn't contain current $items['email'] you may use in_array() function.

Solution:

if ( array_key_exist('email', $item) && !in_array($item['email'], $emails['to']) )
{
    //TODO validate $item['email'] here
    $emails['to'][] = $item['email'];
}

//Result $emails array will be looks like this:
[
 'to' => [ "[email protected]", "[email protected]" ],
 'cc' => []
]

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

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.