创建codeigniter表单选择

So, I am making a from select with the code below:

// Controller Code 
$data['message_to_options'] = array(
   '1' => 'username 1',
   '2' => 'username 2',
);

// View Code 
<?php echo form_dropdown('message_to', $message_to_options); ?>

This all works great. Except the users needs to be dynamically created not hard coded.

I have the following code from ion_auth for code igniter. This gets all the users in the system. Which Is what I want to do.

$users = $this->ion_auth->users()->result();

I just need a way to put them all together. I tried the following but it did not function correctly.

$users = $this->ion_auth->users()->result();

$data['message_to_options'] = array(
   foreach($users $user){
     '$user['id']' => '$user['username']',
   }
);

I didn't think it would work but I figured I would give it a shot. What is the PHP AND Code igniter "legal" way of doing this?

UPDATE

So when I do run this code, I receive this error:

Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')'

This is what I ended up doing:

$users = $this->ion_auth->users()->result();

$data['message_to_options'] = array();

foreach ($users as $user) {
   $userID = $user->id;
   $username = $user->username;
   $data['message_to_options'][$userID] = $username;
}

I declared the user id and the username prior to adding the selection to the array. I also used the correct format as user->id and user->usrname rather than $user['username']

Thanks to @Samutz for making the main components correct!

You can't put a foreach inside an array like that. Also the foreach statement needs "as".

Try this:

$data['message_to_options'] = array();
foreach ($users as $user) {
   $data['message_to_options'][$user->id] = $user->username;
}

I think you are getting result in array rather then object. So you can use following code.

$data['message_to_options'] = array();
foreach ($users as $user) {
   $data['message_to_options'][$user['id']] = $user['username'];
}