How do I populate an address field in php? I want to import data from a external source and so am writing a module that creates content by populating the fields of node bundles in code. I followed this guide which is great. 

So I am working on adding lists of properties (flats and houses) that belong to estates. I am using an address field to store the address and found this comment on the address field module page that tells me that the address field is an array and so I can access each element and set it in turn. 

But what are the address field element and how do I get a list of them? Can they be anything I want? Does the create content form create the array when the form is submitted and therefore the form labels are just a guide or do I have to match them in my code? For example, a UK address has two textboxes for address (address1 and address2) that do not match any of the element names in the module documentation.
But what are the address field element and how do I get a list of them?


You can get a list of the available field columns from addressfield_field_schema():


country
administrative_area
sub_administrative_area
locality
dependent_locality
postal_code
thoroughfare
premise
sub_premise
organisation_name
name_line
first_name
last_name
data



  Can they be anything I want?


Not by default, but as the addressfield module uses ctools plugins, it's easy to extend to add your own if necessary (see the Address Field Phone module for inspiration).


  Does the create content form create the array when the form is submitted and therefore the form labels are just a guide


Exactly right, the addressfield module provides labels and fields based on the currently selected country. They can be whatever suits the locale, but if you look at the names of the form elements, or inspect the field array on form submissions, you'll see they always match the keys from the field schema hook above. There's an excellent answer to a question on SO which goes some way to explaining the reasons for this implementation.

For a UK address specifically, you would populate it roughly like this:

$node-&gt;field_address[LANGUAGE_NONE][0] = array(
  'organisation_name' =&gt; 'Company',
  'first_name' =&gt; 'Joe',
  'last_name' =&gt; 'Bloggs',
  'country' =&gt; 'GB',
  'thoroughfare' =&gt; 'Street address 1',
  'premise' =&gt; 'Street Address 2',
  'locality' =&gt; 'Town/City',
  'administrative_area' =&gt; 'County',
  'postal_code' =&gt; 'SW1 1AA',
);