From Computer Science Wiki
What are we going to learn today?[edit]
- Today in class we will start learning about collections and arrays. THESE ARE DIFFERENT.
- If using weapons as array examples offends you, please use this potion list as an alternative.
<?php
# This file introduces array methods
# this file is named array_methods.php
# We are not using methods in the strictest sense (oop)
# but giving students an idea of how we can interact with an array
$player_inventory = array
(
"sword",
"shield",
"armour",
"potion of healing",
"amulet"
);
echo "You are currently carrying:<br /> ";
for ($i=0; $i < count($player_inventory); $i++)
{
echo $player_inventory[$i] . "<br />";
}
# here is where we add an item to our array:
array_push($player_inventory, "magic ring", "wand");
# now let's look at the new inventory:
echo "<br />This is the new inventory, after we have pushed the new items to the end of the array<br /> ";
for ($i=0; $i < count($player_inventory); $i++)
{
echo $player_inventory[$i] . "<br />";
}
# now we will use common access methods:
echo "<br />Below we see how common access methods work.<br /><br />";
echo "The index is CURRENTly pointing at: " . current($player_inventory);
echo "<br />";
echo "The NEXT index (relative to the current index) is: " . next($player_inventory);
echo "<br />";
echo "The index is now CURRENTly pointing at: " . current($player_inventory);
echo "<br />";
echo "The previous index is: " . prev($player_inventory);
echo "<br />";
echo "The current index is: " . current($player_inventory);
echo "<br />";
echo "The last element in the list is " . end($player_inventory);
echo "<br />";
echo "The current element is: " . current($player_inventory);
echo "<br />";
echo "The first element in the list is: " . reset($player_inventory);
?>
|
Homework[edit]
- Please review our page about collections and arrays
|
How am I being assessed today?[edit]
- You will be formatively assessed in class today
|
Standards we are covering today[edit]
|
As a computer scientist, you have:[edit]
- Confidence in dealing with complexity
- Persistence in working with difficult problems
- Tolerance for ambiguity
- The ability to deal with open-ended problems
- The ability to communicate and work with others to achieve a common goal or solution
|
Credits[edit]
|