Lab 1.13 - Code

-Create and utilize a multidimensional array

Chapter 1 Main





<?php

require_once 'chapter1functions.php';
require_once "../mainfunctions.php";

printHead("1","Lab 1.13", "Create an utilize a multidimensional array");

$productsArray=
array(
"t-shirt"=>array("single"=>9.99,"10 or more"=>8.99,"25 or more"=>7.99,"50 or more"=>6.99,"100 or more"=>5.99),
"ballcap"=>array("single"=>11.99,"10 or more"=>10.99,"25 or more"=>9.99,"50 or more"=>8.99,"100 or more"=>7.99),
"visor"=>array("single"=>8.99,"10 or more"=>8.49,"25 or more"=>7.99,"50 or more"=>7.49,"100 or more"=>6.99),
"magnet"=>array("single"=>4.50,"10 or more"=>4.25,"25 or more"=>4.00,"50 or more"=>3.50,"100 or more"=>3.00),
"coffee mug"=>array("single"=>6.50,"10 or more"=>6.00,"25 or more"=>5.50,"50 or more"=>5.00,"100 or more"=>4.50),
);


//show price for 25 or more visors
echo"<p>Price for 25 or more visors: \$" . $productsArray['visor']['25 or more']."</p>";

//show single prices for each item
echo"<p><strong>Single Prices:</strong>";
echo " Loop through all rows of the products array, print 'single' price only";

foreach($productsArray as $item=>$pricesArray){
    echo"<br>" . $item .":\$" . $pricesArray['single'];
}
echo "</p>";



echo "<p><strong>Ballcap Prices:</strong>";
echo " Loop through one row of the products array";

foreach($productsArray['ballcap'] as $qty=>$price){
    echo"<br>" . $qty . ": \$" . $price;
}
echo "</p>";



$maxPrice = 7.50;

echo"<p><strong>Items available for less than \$$maxPrice:</strong>";
echo " Loop through all rows of the products array and loop through each element of the nested arrays and test against a condition";

foreach($productsArray as $item=>$prices){
    foreach($prices as $qty=>$discPrice){
        if($discPrice < $maxPrice){
            echo"<br>$item - $qty: \$$discPrice";
        }
    }
}




printFootWithCodeExample("1.13code.php");

?>



BACK