Representing a Deck of Cards
Cards date back possibly to the ninth century, the time when sheets of paper first began to be used in China. They followed the path of other inventions from the East – first to the Arab world from where they were then taken to Europe and later into the New World. In its current most popular form, the French Pack, a deck of playing cards has 52 cards divided in four suits, clubs (♣), diamonds (♦), hearts (♥) and spades (♠). Each suit has 13 cards, or faces: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q and K. You can write arrays to hold both suits and faces like this:<?php
$suits = array ("clubs", "diamonds", "hearts", "spades");
$faces = array (1 => "A", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13");
Both are numerically indexed arrays – that is, they have integer-based keys. Because I didn’t explicitly give any keys when I defined $suits
, PHP automatically assigns keys to them starting with 0. So, the value of $suits[0]
is “clubs” and $suits[3]
is “spades”. I did however provide a key to the first element of $faces. PHP assigns each new key by taking the the maximum integer index and adding 1 to it. $faces[1]
is “A”, $faces[2]
is “02”, $faces[3]
is “03” and so on. You’ll notice I forced PHP to start indexing $faces
with 1 so the numerical card faces are identical to their respective keys.
You can use two foreach
loop to create a master array of all 52 cards, each represented as a string in the format face|suit, like this:
<?php
$deck = array();
foreach ($suits as $suit) {
foreach ($faces as $face) {
$deck[] = $face . "|" . $suit;
}
}
The result of the above code is the same as if you had populated $deck
by hand:
<?php
$deck = array("A|clubs", "02|clubs", "03|clubs", "04|clubs", ... "Q|spades", "K|spades");
Some of the more experienced readers may be asking why not use a nested array as opposed to strings, such as:
<?php
$deck = array();
$deck["A"] = array("clubs", "diamonds", "hearts", "spades");
$deck["02"] = array("clubs", "diamonds", "hearts", "spades");
$deck["03"] = array("clubs", "diamonds", "hearts", "spades");
...
Well, that’s the beauty of it: strings can sometimes be treated as non-associative, single level arrays but arrays nonetheless! In fact, the same function used to count the number of elements in an array – count()
– can also be used to count the number of characters in a string! Later you’ll see how to convert strings into arrays.
Dealing a Hand
Let’s begin by shuffling the deck and dealing out a hand of 11 random cards. To do this, you can use the functionarray_rand()
. It returns an array of keys taken randomly from the original. The function was designed to return keys rather than values
so it can work just as efficiently with nested arrays as with single-level arrays, and you can always obtain the values if you know the key.
<?php
$myKeys = array_rand($deck, 11);
$myHand = array();
foreach ($myKeys as $key) {
$myHand[] = $deck[$key];
unset($deck[$key]);
}
Initially, the temporary $myKeys
array is created whose values are 11 random keys found in $deck
. Then a foreach
loop uses the value of $myKeys
to get the corresponding value from $deck
into $myHand
. Of course this does not remove elements from the original deck. If you were to call array_rand()
again, it is entirely possible you could get a few keys indexing again some cards that had already been drawn! To make sure this doesn’t happen, unset()
is called to delete the element from $deck
to make sure it can’t be reused.
To find out whether a card, say “06|hearts” (6♥), is in the hand that was dealt, you can use the in_array()
function. It accepts a needle first, the desired value to search for, and then the haystack, the array in which to search.
<?php
if (in_array("06|hearts", $myHand)) {
echo "I found it!";
}
As a side note about needles and haystacks, evangelists of other languages love to find faults with PHP (and of course the reverse is also true!). The only criticism I could never refute was PHP’s irritating inconsistent parameter order between similar functions. Some functions, like in_array()
, accept the needle first, while other functions accept the haystack first. I know long-time PHP developers who still have trouble remembering which order some functions use, so don’t be too put off if you find yourself always needing to check the online documentation.
in_array()
returns only whether something was found in an array, not the key of the value. Most of the time this is sufficient. But if you need to know the key as well, consider using array_search()
.
Good housekeeping is important, and thanks to the face|suit manner in which the cards are represented, sorting them is as easy as using sort()
. This function orders the elements of the array in ascending alphabetical and numeric order:
<?php
sort($myHand);
The sort()
function is peculiar in that it operates on its own argument! If you wanted to preserve the original order of $myHand
you would have to copy it to another variable before sorting:
<?php
$preservedHand = $myHand;
sort($myHand);
Melds and Discards
Buraco, like rummy, is a game of runs – sequences laid down on the table. The act of removing cards from the hand onto a run is called melding. For example, cards 9♦, 10♦, J♦ and Q♦ if they were in$myHand
could be melded to make a run. Programatically this means removing the cards from $myHand
and putting them into a new array, $myRuns
. You can use the function array_slice()
which returns a copy of a range of elements from an array, very much like the way substr()
works on strings. Assuming the sequences occupies positions 0 through 3 in $myHand
:
<?php
$myRuns = array();
$myRuns[] = array_slice ($myHand, 0, 4);
$myHand = array_slice($myHand, 5, 7);
The first line creates the array $myRuns
and the second one adds to it a sub-array composed of 4 elements from $myHand
beginning at index 0, which is the first position in the array. array_slice()
does not delete the slice from the original array, which is a problem here since you need to remove the cards from your hand that you meld or discard. The solution here is to use array_slice()
a second time to reassign the other elements of $myHand
to itself.
At the beginning of his turn, a player must either pick up one card from the stock or all cards from the discard pile and add them to his hand. Both options can be done using the array_merge()
function. The function returns an array composed of all the elements from the given arrays in the order they were provided. For example, let’s say the state of the game looks like this:
Player’s Hand: 2♣, 5♥, 6♠, 6♥, 7♦, 8♥, K♠Run: 9♦, 10♦, J♦, Q♦
Discard Pile: 7♠, J♠, 4♥, 4♦ To pick up all the cards in the discard pile and add them to the players hand, you could write:
<?php
$myHand = array_merge($myHand, $discarded);
Afterwards, $myHand
would contain:
array("02|clubs", "5|hearts", "06|spades", "06|hearts", "07|diamonds", "08|hearts", "13|spades", "07|spades", "11|spades", "04|hearts", "04|diamonds")
Showing the Hand
It’d be nice to display one or more cards in the browser. My strategy is to use the face|suit notation to create a number of HTML image tags that will show face and suit graphics. While it is not likely an effective solution for a real-world application, it does afford me an opportunity to show you one last function,explode()
.
<?php
foreach ($myHand as $card) {
$tmpArray = explode("|", $card);
echo '<img src="img/face_' . $faces[$tmpArray[0]] . '.png">";
echo '<img src="img/suit_' . $suits[$tmpArray[1]] . '.png">";
echo " "; // Just a blank space for visual separation
}
The foreach
loops through all of the cards in $myHand
and returns one face|suit value each iteration as $card
. The explode()
function takes each $card
value and splits it into two pieces using the vertical bar as the separator. It then returns an array with as many elements as there are parts. I’m using a vertical bar here, but explode()
will work with any character or sequence of characters.
Summary
PHP has over 70 array-related functions. In this tutorial you’ve seen how to use a mere handful of them to perform some basic array manipulations. These functions are tools you’ll use often to solve programming problem or, as I prefer to call them, challenges. It’s important to develop a familiarity with them so that when you plan your script you can make sound decisions based on the results you need. Image via Cindy Haggerty / ShutterstockFrequently Asked Questions (FAQs) about PHP Array Handling Functions
What are the different types of PHP array functions?
PHP provides a wide range of array functions that allow you to manipulate and handle arrays effectively. These include array_change_key_case(), array_chunk(), array_column(), array_combine(), array_count_values(), array_diff(), array_fill(), array_filter(), array_flip(), array_intersect(), array_key_exists(), array_keys(), array_map(), array_merge(), array_multisort(), array_pad(), array_pop(), array_push(), array_rand(), array_reduce(), array_reverse(), array_search(), array_shift(), array_slice(), array_splice(), array_sum(), array_unique(), array_unshift(), array_values(), array_walk(), and many more. Each function has a unique purpose and usage, which can be learned through practice and experience.
How can I use the array_map() function in PHP?
The array_map() function in PHP is used to apply a callback function to every element of the given array. The callback function is applied to the array in a way that it changes the original array’s elements. Here’s an example:<?php
function square($n)
{
return($n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("square", $a);
print_r($b);
?>
In this example, the square function is applied to each element of array $a, and the result is stored in array $b.
How does the array_diff() function work in PHP?
The array_diff() function in PHP is used to compare the values of two or more arrays and return the differences. It compares the values of the first array with the values of following arrays, and returns an array that contains all the entries from the first array that are not present in any of the other arrays. Here’s an example:<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
In this example, the array_diff() function compares $array1 and $array2, and returns an array that contains the values from $array1 that are not present in $array2.
What is the purpose of the array_keys() function in PHP?
The array_keys() function in PHP is used to return all the keys or a subset of the keys of an array. It returns an array of all the keys present in the input array. If a value is specified, it will return only the keys for that value. Here’s an example:<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
?>
In this example, the array_keys() function returns an array that contains all the keys of the $array.
How can I use the array_merge() function in PHP?
The array_merge() function in PHP is used to merge one or more arrays into one array. The function appends the values of the second array to the first array. If the arrays have the same string keys, then the later value will overwrite the previous one. Here’s an example:<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
In this example, the array_merge() function merges $array1 and $array2 into one array, and the result is stored in $result.
What is the use of the array_search() function in PHP?
The array_search() function in PHP is used to search for a specific value in an array. If the value is found, the function returns the first corresponding key. If the value is not found, it returns FALSE. Here’s an example:<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array);
echo $key;
?>
In this example, the array_search() function searches for the value ‘green’ in the $array, and returns its key.
How does the array_slice() function work in PHP?
The array_slice() function in PHP is used to extract a slice of an array. It returns the sequence of elements from the array array as specified by the offset and length parameters. Here’s an example:<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2);
print_r($output);
?>
In this example, the array_slice() function returns the elements from the third position of the $input array.
What is the purpose of the array_unique() function in PHP?
The array_unique() function in PHP is used to remove duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Here’s an example:<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
In this example, the array_unique() function removes the duplicate values from the $input array.
How can I use the array_values() function in PHP?
The array_values() function in PHP is used to return all the values of an array. The function returns an indexed array of values. Here’s an example:<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>
In this example, the array_values() function returns an array that contains all the values of the $array.
What is the use of the array_walk() function in PHP?
The array_walk() function in PHP is used to apply a user-defined function to each element of an array. The function is applied to the array in a way that it changes the original array’s elements. Here’s an example:<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a=array("a"=>"red","b"=>"blue","c"=>"green");
array_walk($a,"myfunction");
?>
In this example, the myfunction is applied to each element of array $a.
Armando Jeronymo has a B.Sc. in Electronics Engineering from Rio de Janeiro's Ursuline University and a US Associate degree in Risk Management. He started coding in a TI-59 calculator in the early 80's. He then learned and forgot Sinclair Basic, Fortran, Algol and MS Basic. For ten years he did not write code but worked with insurance and reinsurance in his native Brazil and London. Then he went back to programming to find out everything was different. He began by getting to know HTML to create some material on aviation instruction. He then learned Delphi's Pascal in order to create some financial applications. He began experimenting with server-side languages about the turn of the Century, first with ASP but soon moving to PHP. He wrote his first MySQL web application in about 2004. He switched to Linux in 2005 (Ubuntu 5.10 to be more exact) and has been developing LAMP systems since then. He is a Private Pilot and, whenever possible, a FlightGear flyer. He loves classical music and jazz (including Bossa Nova) and old movies. Besides computing he is professionally interested in business management and a follower of Peter Drucker's teaching. Married and father of a girl he is also an active member of his Rotary Club.