Categories
Code Academy php unit Samuel

PHP Unit 11 advanced Arrays and Maps

lesson 1 Review of Arrays:


$fruits = array ('bananas', 'apples', 'pears');
echo $fruits[1]; /* Your code here! */

Lesson 2 Associative Arrays


// This is an array using integers as the indices...
$myArray = array(2012, 'blue', 5);

// ...and this is an associative array:
$myAssocArray = array('year' => 2012,
'colour' => 'blue',
'doors' => 5);

// This code will output "blue"...
echo $myArray[1];
echo '
';

// ... and this will also output "blue"!
echo $myAssocArray['colour'];

Lesson 3 Using Arrays as Maps


// This is an array using integers as the indices.
// Add 'BMW' as the last element in the array!
$car = array(2012, 'blue', 5, 'BMW');

// This is an associative array.
// Add the make => 'BMW' key/value pair!
$assocCar = array('year' => 2012,
'colour' => 'blue',
'doors' => 5,
'make' => 'BMW');

// This code should output "BMW"...
echo $car[3];
echo '
';

// ...and so should this!
echo $assocCar['make'];

**this lesson errors out when you try to submit it. Add a line after the ‘make’ line, something like ‘make’ => ‘BMW’);+ and hit submit. When it errors out then delete the + after the ; and hit submit again. Seems to be a bug with code academy validation.

Lesson 4 Accessing Associative Arrays


// This is an array using integers as the indices...
$myArray = array(2012, 'blue', 5, 'BMW');

// ...and this is an associative array:
$myAssocArray = array('year' => 2012,
'colour' => 'blue',
'doors' => 5,
'make' => 'BMW');

// This code will output "blue".
echo $myArray[1];
echo '
';

// Add your code here!
echo $myArray[1];
echo $myAssocArray['make'];

Lesson 5 Iterating Over Associative Arrays


$food = array('pizza', 'salad', 'burger');
$salad = array('lettuce' => 'with',
'tomato' => 'without',
'onions' => 'with');

// Looping through an array using "for".
// First, let's get the length of the array!
$length = count($food);

// Remember, arrays in PHP are zero-based:
for ($i = 0; $i < $length; $i++) { echo $food[$i] . '
';
}

echo '

I want my salad:
';

// Loop through an associative array using "foreach":
foreach ($salad as $ingredient=>$include) {
echo $include . ' ' . $ingredient . '
';
}

echo '

';

// Create your own array here and loop
// through it using foreach!

$hockeyTeams = array (
'blues' => 'great',
'hawks' => 'bad',
'wings' => 'worst');

//loop through array using "foreach"
foreach ($hockeyTeams as $team=>$include) {
echo $include . ' ' . $team . '
';

}

Lesson 6 Multidimensional Arrays

$deck = array(array('2 of Diamonds', 2),
array('5 of Diamonds', 5),
array('7 of Diamonds', 7),
array('8 of Hearts', 8));

// Imagine the first chosen card was the 7 of Diamonds.
// This is how we would show the user what they have:
echo 'You have the ' . $deck[2][0] . '!';

Lesson 7 putting it all togather


// On the line below, create your own associative array:
$myArray = array (
'cubs' => 'should hopefully be out soon');

// On the line below, output one of the values to the page:
echo $myArray['cubs'];

// On the line below, loop through the array and output
// *all* of the values to the page:

foreach ($myArray as $baseballTeam=>$list){
echo 'the' . ' ' . $baseballTeam . ' ' . $list;
}