Category: PHP Arrays

Using list() With explode() In PHP

8 September, 2009 | PHP Arrays | No comments

A simple way to convert a string into a set of variables is through the use of the explode() and list() functions. list() is a language construct (not really a function) that will convert an array into a list of variables. For example, to convert a simple array into a set of variables do the following:

list($variable1, $variable2) = array(1, 2);

In this example $variable1 now contains the value 1 and $variable2 contains the value 2. This can be adapted to use the explode() function to take a string and convert it into a set of variables. An example of this in use might be when dealing with addresses, simply explode the string using the comma and you have a set of variables.

$address = '123 Fake Street, Town, City, PO3T C0D3'; list($street, $town, $city, $postcode) = explode(',', $address);

You can now print out parts of the address like this:

echo 'Street: ' . $street . '<br />Post Code' . $postcode . '.';

The good thing about using this code is that even if part of the address isn't present you will still get a variable for that space. Altering the previous example to remove the town and the city like this:

$address = '123 Fake Street, , , PO3T C0D3'; list($street, $town, $city, $postcode) = explode(',', $address);

Has the effect of creating empty $town and $city variables. They should always be present as long as your address has the same number of parts.

Written by Philip Norton.

Print Array Without Trailing Commas In PHP

24 April, 2009 | PHP Arrays | No comments

I have previously talked about Removing commas from the end of strings, but it is also possible to use the implode() function to do the same sort of thing.

implode() takes two parameters, the separator and the array, and returns a string with each array item separated with the separator. The following example shows how this function works.

$array = array(1,2,3,4,5,6); $list = implode(',', $array);

The $list variable will now contain the string "1,2,3,4,5,6". However, things tend to become messy again when you have an array with empty items in it.

$array = array(1,2,3,4,5,6,'','',''); $list = implode(',', $array);

The $list variable will now contain the string "1,2,3,4,5,6,,". So to solve this issue we need to use the array_filter() function to clear out any blank array items before passing the output to the implode() function. The following example shows this in action.

$array = array(1,2,3,4,5,6,'','',''); $list = implode(',', array_filter($array));

The $list variable will now contain the string "1,2,3,4,5,6", which is the string we are looking for.

Written by Philip Norton.

PHP Array Of Australian States

23 April, 2009 | PHP Arrays | No comments

Use the following array to print out a list of Australian states. $australian_states = array(     "NSW"=>"New South Wales",     "VIC"=>"Victoria",     "QLD"=>"Queensland",     "TAS"=>"Tasmania",     "SA"=>"South Australia",     "WA"=>"Western Australia",     "NT"=>"Northern Territory",     "ACT"=>"Australian Capital Terrirory");

Written by Philip Norton.

PHP Array Of Canadian States

23 April, 2009 | PHP Arrays | 2 comments

Use the following array to print out a list of Canadian states, also know as provinces.

$canadian_states = array(      "BC"=>"British Columbia",      "ON"=>"Ontario",      "NL"=>"Newfoundland and Labrador",      "NS"=>"Nova Scotia",      "PE"=>"Prince Edward Island",      "NB"=>"New Brunswick",      "QC"=>"Quebec",      "MB"=>"Manitoba",      "SK"=>"Saskatchewan",      "AB"=>"Alberta",      "NT"=>"Northwest Territories",      "NU"=>"Nunavut"     "YT"=>"Yukon Territory");

Here is the same array, but with the French versions of the states.

$canadian_states = array(      "AB"=>"Alberta"     "BC"=>"Colombie-Britannique"     "MB"=>"Manitoba"     "NB"=>"Nouveau-Brunswick"     "NL"=>"Terre-Neuve-et-Labrador"     "NS"=>"Nouvelle-Écosse"     "NT"=>"Territoires du Nord-Ouest"     "NU"=>"Nunavut"     "ON"=>"Ontario"     "PE"=>"Île-du-Prince-Édouard"     "QC"=>"Québec"     "SK"=>"Saskatchewan"     "YT"=>"Yukon");

Written by Philip Norton.

PHP Array Of USA States

23 April, 2009 | PHP Arrays | 1 comment

Use the following array if you want to print out a list of USA states either as a list, or as a select box.

$state_list = array('AL'=>"Alabama",       'AK'=>"Alaska",       'AZ'=>"Arizona",       'AR'=>"Arkansas",       'CA'=>"California",       'CO'=>"Colorado",       'CT'=>"Connecticut",       'DE'=>"Delaware",       'DC'=>"District Of Columbia",       'FL'=>"Florida",       'GA'=>"Georgia",       'HI'=>"Hawaii",       'ID'=>"Idaho",       'IL'=>"Illinois",       'IN'=>"Indiana",       'IA'=>"Iowa",       'KS'=>"Kansas",       'KY'=>"Kentucky",       'LA'=>"Louisiana",       'ME'=>"Maine",       'MD'=>"Maryland",       'MA'=>"Massachusetts",       'MI'=>"Michigan",       'MN'=>"Minnesota",       'MS'=>"Mississippi",       'MO'=>"Missouri",       'MT'=>"Montana",     'NE'=>"Nebraska",     'NV'=>"Nevada",     'NH'=>"New Hampshire",     'NJ'=>"New Jersey",     'NM'=>"New Mexico",     'NY'=>"New York",     'NC'=>"North Carolina",     'ND'=>"North Dakota",     'OH'=>"Ohio",       'OK'=>"Oklahoma",       'OR'=>"Oregon",       'PA'=>"Pennsylvania",       'RI'=>"Rhode Island",       'SC'=>"South Carolina",       'SD'=>"South Dakota",     'TN'=>"Tennessee",       'TX'=>"Texas",       'UT'=>"Utah",       'VT'=>"Vermont",       'VA'=>"Virginia",       'WA'=>"Washington",       'WV'=>"West Virginia",       'WI'=>"Wisconsin",       'WY'=>"Wyoming");

If you want to reverse this then use the following code.

$reverse = array(); foreach ( $state_list as $key=>$state ) {     $reverse[$state] = $key; } $state_list = $reverse;

Written by Philip Norton.