PHP array, using key without a specific key value

Solution for PHP array, using key without a specific key value
is Given Below:

I have a simple foreach loop here that is working on one email client and not the other. The issue is that one mail client sends an array that has a key value of “personal” while the other doesn’t. However, they both have the “host” and “mailbox” values at all times, no matter what.

My loop logic has been set like this:

        if(isset($header->cc)){
            foreach($header->cc as $key){
                $ccArray[$key->personal] = $key->mailbox . "@" . $key->host;
            }
        }

This works for the one client but obviously not the other. However, if I remove the personal section where I set my array key, I will get an illegal offset error.

Is there a way I can continue setting my array with the mailbox and host even if the personal key/index isn’t set or available?

I think you are looking for something like this:

   if(isset($header->cc)){
        foreach($header->cc as $key){
            $ccArray[] = $key->mailbox . "@" . $key->host;
        }
    }