How to modify an array

$names = array('Mike', 'Mike', 'Mike', 'Anne', 'Joel', 'Joel'); $names = array_unique($names); // Mike, Anne, Joel $names = array_reverse($names); // Joel, Anne, Mike shuffle($names); // Mike, Joel, Anne (for example)

How to modify an associative array

$tax_rates = array('NC' => 7.75, 'NY' => 8.875, 'CA' => 8.25);
$tax_rates = array_reverse($tax_rates, true);

How to get random keys from an array

$names = array('Mike', 'Anne', 'Joel', 'Ray', 'Pren');
$key = array_rand($names); // 2 (for example)
$names_rand = array_rand($names, 3); // 0, 1, 3 (for exmple)

How to shuffle and deal a deck of cards

// Create the deck of cards
$faces = array('2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A');
$suits = array('h', 'd', 'c', 's');
$cards = array();
foreach($faces as $face) {
  foreach($suits as $suit) {
    $cards[] = $face . $suit;
  }
}
// Shuffle the deck and deal the cards
shuffle($cards);
$hand = array();
for ($i = 0; $i < 5; $i++) {
  $hand[] = array_pop($cards);
}
echo implode(',', $hand); // 9c,6d,Ks,4c,7h (for example)

Hand = 6c,2h,2s,Kh,Qd

Back