PHP Arrays

Posts about arrays in PHP

Using list() With explode() In PHP

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:

Print Array Without Trailing Commas In PHP

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.

PHP Array Of Australian States

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");

PHP Array Of Canadian States

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

PHP Array Of USA States

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

Delete Trailing Commas In PHP

Converting an array of information into a string is easy, but when you are doing this for insertion into a database having trailing commas is going to mess up your SQL statements.

Take the following example, which takes an array of values and converts them into a string of values. This practice is quite common in PHP database manipulation.