Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
<html>
<head>
<title>Array Review</title>
</head>
<body>
<p>
<?php
$fruits = array("bananas", "apples", "pears");
echo 'I love eating ' . $fruits[1] . ' too!';
?>
</p>
<p>
<?php
// 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 '<br />';
// ... and this will also output "blue"!
echo $myAssocArray['colour'];
?>
</p>
<p>
<?php
// 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 '<br />';
// ...and so should this!
echo $assocCar['make'];
?>
</p>
<p>
<?php
// This is an array using integers as the indices...
$myCar = array(2012, 'blue', 5, 'BMW');
// ...and this is an associative array:
$myCarAssocArray = array('year' => 2012,
'colour' => 'blue',
'doors' => 5,
'make' => 'BMW');
// This code will output "blue".
echo $myCar[1];
echo '<br />';
// Add your code here!
echo 'I have a ' . $myCarAssocArray['year'] . ' ' . $myCarAssocArray['make'] . '. It is ' . $myCarAssocArray['colour'] . ' and has ' . $myCarAssocArray['doors'] . ' doors.';
?>
</p>
<p>
<?php
$food = array('pizza', 'salad', 'burger');
$salad = array('lettuce' => 'with',
'tomato' => 'without',
'onions' => 'with');
$pizza = array('cheese' => 'with',
'pepperoni' => 'without',
'mushrooms' => 'with');
$burger = array('cheese' => 'without',
'bread' => 'without',
'bacon' => 'with',
'onions' => 'without',
'lettuce' => 'with',
'mayo' => 'with',
'pickles' => '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] . '<br />';
}
echo '<br /><br />I want my salad:<br />';
// Loop through an associative array using "foreach":
foreach ($salad as $ingredient=>$include) {
echo $include . ' ' . $ingredient . '<br />';
}
// Create your own array here and loop
// through it using foreach!
echo '<br /><br />I want my pizza:<br />';
foreach ($pizza as $ingredient=>$include) {
echo $include . ' ' . $ingredient . '<br />';
}
echo '<br /><br />I want my burger:<br />';
foreach ($burger as $ingredient=>$include) {
echo $include . ' ' . $ingredient . '<br />';
}
?>
</p>
<p>
<?php
$deck = array(array('2 of Diamonds', 2),
array('5 of Diamonds', 5),
array('7 of Diamonds', 7),
array('Jack of Hearts', 10));
// 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[3][0] . '!';
?>
</p>
<p>
<?php
// On the line below, create your own associative array:
$myArray = array('pizza', 'rice', 'pasta', 'chips');
$pizza = array('gluten' => 'wheat',
'dairy' => 'cheese');
// On the line below, output one of the values to the page:
// On the line below, loop through the array and output
// *all* of the values to the page:
// Looping through an array using "for".
// First, let's get the length of the array!
$length = count($myArray);
// Remember, arrays in PHP are zero-based:
for ($i = 0; $i < $length; $i++) {
echo $myArray[$i] . '<br />';
}
echo '<br /><br /> This contains contains:<br />';
// Loop through an associative array using "foreach":
foreach ($pizza as $ingredient=>$include) {
echo $include . ' ' . $ingredient . '<br />';
}
?>
</p>
</body>
</html>