Archive

Posts Tagged ‘array’

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: , , ,

PHP Script To Select A Person To Make The Tea

April 6th, 2009 1 comment

In any office there can be arguments about who will make the next round of tea. The following script will allow you to randomly pick a person who is going to make the tea. Rather than have a script that did this once and threw away the information I thought it would be a good idea to use cookies to save the form data for the next time you want to pick a person to make the tea. This is a good exercise if you are trying to understand how cookies work.

First, we will need to variables, the first is an array of people and the second is the number of people in the office.

$people = array();
$number = 10;

We can now build the form that will contain all of our names. Here we just cycle through a simple for loop and if an array item exists in the $people array for the value of $i then we use this in our form.

<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
for ( $i=0 ; $i<$number ; $i++ ) {
    if ( isset($people[$i]) ) {
        echo '<input type="text" name="people[]" value="'.$people[$i].'" /><br />';        
    } else {
        echo '<input type="text" name="people[]" value="" /><br />';    
    }
}
?>
<input type="submit" value="Save &amp; Run" name="submit" />
</form>

Next we need to grab the variables coming from the form and store them in a array, because we used a name with square brackets we can access the people GET variable as if it were an array.

if ( isset($_GET['people']) ) {
  $people = $_GET['people'];
}

We can now select who will be making the tea. Here I use a combination of array_filter() to remove all blank entries in the array and array_rand() to randomly select a key from the array. The array_rand() function returns NULL if it is given a blank array so we need to account for people first visiting the page.

if ( $who = array_rand(array_filter($people)) ) {
    $teaMaker = $people[$who];
    echo '<p>'.$teaMaker.' will be making the tea!</p>';
}

Enabling cookies with this script is quite easy and involves only a small amount of code addition. First we need to create a cookie if the people variable exists. Because this is an array and the setcookie() function requires a string we need to use the serialize() function to convert the $people array into a string value.

if ( isset($_GET['people']) ) {
    $people = $_GET['people'];
    setcookie("teaCookie", serialize($people));
}

Now that we have created our cookie we need to retrieve it again when the page reloads. All cookies are kept by reference in the $_COOKIE superglobal array. Once we are sure that the cookie exists we can user unserialize() to convert our serialized string into an array again.

if ( isset($_COOKIE['teaCookie']) ) {
    $people = unserialize($_COOKIE["teaCookie"]);
}

We now have the $people array in its original state.

Finally, rather than continuously setting and unsetting the cookie I have added a link that will allow you to pick another person from the list, without running the form. We first need to make sure that our cookie exists before allowing this link as it will do nothing.

<?php if ( isset($_COOKIE['teaCookie']) ) { ?>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Pick Again!</a></p>
<?php }?>

Of course this script isn’t all that useful if you either don’t drink tea or work alone, but it can easily be adapted to other functions. You can host this script on your Intranet or website for internal usage if you wish, but I have uploaded a version of the tea picker for you to play with, along with the source code.