How can I create a new user—with Profile 2 fields—programmatically? I am writing a module where I need to create a new user and save his profile fields using Profile 2.

This is the code I'm using, but it's not working:

$edit = array(
  'name' =&gt; $submission-&gt;data[1]['value'][0],
  'pass' =&gt; user_password(),
  'mail' =&gt; $email,
  'init' =&gt; $email,
  'status' =&gt; 1,
  'roles' =&gt; $role_id,
  'profile_first_name' =&gt; $submission-&gt;data[1]['value'][0],
  'profile_surname' =&gt; 'ag',
  'profile_email' =&gt; $email,
  'access' =&gt; REQUEST_TIME,
);

$new_user = user_save(drupal_anonymous_user(), $edit/*,'customer_profile'*/);


What am I doing wrong?
I load the profile object:

$profile = profile2_by_uid_load($user-&gt;uid, '&lt;role type here&gt;');


Then I edit the $profile object.

I have a Rules component that saves the profile object. I forget why I have the rule but I remember that it requires quite a bit of code and database queries to get it right.

I set the Rules component to take the $profile object as an argument / parameter and invoke it like this.

    rules_invoke_component('rules_machine_name_this_is_the_component_url_too', $profile);