Archive

Archive for the ‘PHP Arrays’ Category

Print Array Without Trailing Commas In PHP

April 24th, 2009 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.

PHP Array Of Australian States

April 23rd, 2009 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");

Categories: PHP Arrays Tags: , , ,

PHP Array Of Canadian States

April 23rd, 2009 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");

Categories: PHP Arrays Tags: , , ,

PHP Array Of USA States

April 23rd, 2009 No comments

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;

Categories: PHP Arrays Tags: , , ,

Delete Trailing Commas In PHP

April 14th, 2009 1 comment

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.

$values = array('one', 'two', 'three', 'four', 'five');
$string = '';
 
foreach ( $values as $val ) {
    $string .= '"'.$val.'", ';
}
 
echo $string; // prints "one", "two", "three", "four", "five",

Obviously we need to strip the trailing comma from the end of this string. To do this you can use the following function.

function deleteTrailingCommas($str)
{
    return trim(preg_replace("/(.*?)((,|s)*)$/m", "$1", $str));
}

This function uses a regular expression to match for one or more commas or spaces after the main bulk of text and before the end of the string and prints out the main bulk of text. The trailing commas are not returned.

Here is another example:

$string = '"one", , ,  , , , ,,';
echo $string;
$string = deleteTrailingCommas($string);
echo $string;

This prints out the following:

"one", , ,  , , , ,,
"one"