0

I have an object with a property:

protected $recipients = array();

In one of my methods I set the contents of the $recipients property with an associative array of data using this method:

public function setRecipientData($recipientData){
    foreach($recipientData as $key => $value){
        $this->recipients[$key] = $value;
    }
}    

When I dump the contents of $this->recipients, the array ends up looking like this:

array (size=2)
  0 => 
    array (size=6)
      'email'       => string 'info@mycompany.com' (length=29)
      'userEmail'   => string 'xxx@yyy.com' (length=11)
      'first_name'  => string 'Test' (length=4)
      'last_name'   => string 'User' (length=4)
      'jobTitle'    => string 'Customer User' (length=13)
      'phoneNumber' => string '123.456.7890' (length=12)

I also have a property that will extract only the email addresses from the property like this:

private function getRecipientEmails(){
    $emails = array();
    foreach($this->recipients as $recipient){
        $emailPair = array('email' => $recipient['userEmail']);
        $emails[] =  $emailPair;
    }
    return $emails;
}

The problem that I'm running into is that when I run the method getRecipientsEmails() I get the error:

Undefined index: userEmail

I don't understand why it's not seeing the index 'userEmail'. If I dump the nested array it shows a value:

private function getRecipientEmails(){
    $emails = array();
    foreach($this->recipients as $recipient){
        dd($emailPair = array('email' => $recipient['userEmail']));
        $emails[] =  $emailPair;
    }
    return $emails;
}

Result:

array (size=1)
  'email' => string 'xxx@yyy.com' (length=20)

I've tried dumping the autoload with composer but it makes no difference.

Can anyone help point me in the right direction??

EDIT: Sorry, I realized that I wrote the wrong method into my original post. I've added the original getRecipientEmails() method and the dump that shows the data.

Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137
  • `$recipient` is your index now and not an array that leads to Undefined index: userEmail it's not a multidimensional array when you transformed it, if you want to access it (your processed array) `this->recipients['userEmail']` should do it – Drixson Oseña May 23 '14 at 02:53
  • @DrixsonOseña, I realized I posted the wrong version of the method in my original post. I don't know if that changes anything behind your comment. Also, my hope is that the recipients property would be a multidimensional array as it could possibly hold the data for multiple contacts. – Chris Schmitz May 23 '14 at 03:57
  • @John Conde, You marked this post as a duplicate last night. I read through the post that you marked as an answer. I may be missing it, but I don't understand how this solves the issue I'm having. I updated my post last night to include the original version of my method which does include a variable initialization for the `email` array in the `getRecipientEmails()` method and it still does not fix the issue. If you were referring to the recipients property, that property is initialized by default. Am I still missing something here? – Chris Schmitz May 23 '14 at 14:09

0 Answers0