Loop through an array inside a class using foreach [closed]

You can’t have code directly in the class definition. If you want it on instantiation, put it in the __construct function.

Also, be aware that you were missing a semicolon and .= is used to append to strings, not to add to arrays. I’ve fixed those as well:

<?php
class myclass {
  private $arr = array(
    'emails' => array(
      '[email protected]',
      '[email protected]',
      '[email protected]'
    )
  );
  protected $emails = array();

  function __construct() {
    foreach($this->arr['emails'] as $email) {
        $emails[] = $email;
      }
    }
}