<?php
  
if (isset($_GET['source'])) {
        
highlight_file($_SERVER['SCRIPT_FILENAME']);
        exit;
  }
?>
<html>
<head>
<title>Basic PHP Example</title>
    
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="theme-color" content="#B5C7DD" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../css/reset.css">
        <link rel="stylesheet" href="../css/mandy.css">

<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
</head>
<body>
    
<p class="pad-top">    
    
<?php   # need this to be able to view source in the below anchor tag
  
$urlPath=dirname($_SERVER['SCRIPT_URL']).'/'
  
$labsPath=$urlPath.'labs/'
?>     

    <a href="<?=$urlPath?>index.php?source" target="labs">Lab 4 Source code</a>

</p>        
<pre>

<?php   
    
// Given Array
    
$ceu = array( "Italy"=>"Rome""Luxembourg"=>"Luxembourg"
         
"Belgium"=> "Brussels""Denmark"=>"Copenhagen"
         
"Finland"=>"Helsinki""France" => "Paris"
         
"Slovakia"=>"Bratislava""Slovenia"=>"Ljubljana"
         
"Germany" => "Berlin""Greece" => "Athens"
         
"Ireland"=>"Dublin""Netherlands"=>"Amsterdam"
         
"Portugal"=>"Lisbon""Spain"=>"Madrid""Sweden"=>"Stockholm",
         
"United Kingdom"=>"London""Cyprus"=>"Nicosia"
         
"Lithuania"=>"Vilnius""Czech Republic"=>"Prague"
         
"Estonia"=>"Tallin""Hungary"=>"Budapest""Latvia"=>"Riga"
         
"Malta"=>"Valetta""Austria" => "Vienna""Poland"=>"Warsaw") ;
    
    
//  Task 1  
       
echo "<h4>Task 1</h4>";
         
    
//  Write php code which produces the following output:

    #   The capital of Netherlands is Amsterdam 
       
echo "The capital of Netherlands is: {$ceu["Netherlands"]}\n";
       
    
#   The capital of Greece is Athens 
       
echo "The capital of Greece is: {$ceu["Greece"]}\n";
       
    
#   The capital of Germany is Berlin
       
echo "The capital of Germany is: {$ceu["Germany"]}\n";
       
    
//  Task 2  Sort the list by key
       
echo "<h4>Task 2</h4>";
       
    
/*  
        Sort the list by key (using one of the array sort functions).
        Output your array values as follows:

        print_r($ceu);
    */
    #  sorting by key (assume sorted keys by aphabetical order)  
       
ksort($ceu);
    
#  printing sorted array   
       
print_r($ceu);  

    
//  Task 3  Sort the list by value
       
echo "<h4>Task 3</h4>"
     
    
#  using temporary array = original $ceu for destructive procedure  
       
$temparray $ceu;
       
    
#  sorting by value (assume sorted values by aphabetical order) while maintaining associations of keys and values 
       
asort($temparray);
    
#  printing sorted array   
       
print_r($temparray);
       
       
    
//  Task 4  Create multi-dimensional arrays
       
echo "<h4>Task 4</h4>";
       
//     First, create multi-dimensional arrays by creating some arrays 
       
echo "<h5>1. multi-dimensional arrays</h5>";
       
$countFrench = ["zero""un""deux""trois""quatre""cinq""six""sept""huit""neuf""dix"];
         
       
$countPortug = ["zero""un""dois""trĂªs""quatro""cinco""seis""sete""oito""nove""dez"];
         
       
$countEspano = ["cero""uno""dos""tres""cuatro""cinco""seis""siete""ocho""nueve""diez"];
       
    
#  and setting those arrays as values when creating another array.  
       
$countPolyGl = [$countFrench$countPortug$countEspano];
       
    
#  printing results with array names as headers (but with space after $ or else it'll just print the string Array)   
       
echo "<h4>  $ countFrench  </h4>"
       
print_r($countFrench);
       echo 
"<h4>  $ countPortug  </h4>";  
       
print_r($countPortug);
       echo 
"<h4>  $ countEspano  </h4>";  
       
print_r($countEspano);
       echo 
"<h4>  $ countPolyGl  </h4>";  
       
print_r($countPolyGl);
/*
       Second, create a multi-dimensional array on one line using the array notation using a single variable assignment.  (i.e.   $mynewarray = ....;)
*/     
echo "<h5>2. In one line only</h5>";
       
       
$pet = array(
           
"pet1" => array("name" => "Cloudy""type" => "dog""color" => "white"),
           
"pet2" => array("name" => "Cait""type" => "cat""color" => "orange"),
                   );
       
       echo 
"<h4>  $ pet  </h4>";  
       
print_r($pet);
       
       
#    using pet1 and pet2 instead of indexes or strings results in this warning:  Notice: Use of undefined constant pet1 - assumed 'pet1' in /students/mchen75/public_html/cs130a/lab4/index.php on line 115 

/*
       Output your arrays using the print_r function and make comments/notes about what your thoughts about creating arrays.
*/     
echo "<h5>3. comments</h5>";
       
       
/* 
       even when echoing or printing array variable names between html tags, if the exact name is written such as <h4>$countPortug</h4>, the string "Array" will be printed instead, so for all cases above I had to put a space in between to keep track of arrays and their variable names when printed out. For assignment of multidimensional array into a single line, variable names starting with $ cannot be utilized, or an error returns and the page fails to render.
       
       Why is sorting by value destructive but not sorting by key if the associations between key and value are maintained?
       
       Do multidimensional arrays have to have the same keys or can they have missing keys or keys/values not in the same order? It makes sense to keep them consistent regardless.
       */
       

       
       
    //  Task 5  Delete element of an array
       
echo "<h4>Task 5</h4>";       
       
    
#   Using our original $ceu array from Task 1, delete "Lithuania" from the array.
       
$ceu2 $ceu;        #   making copy of $ceu         
       
unset($ceu2["Lithuania"]);    #   the single element "Lithuania" is deleted from $ceu
               
    #   Print the array using the print_r function.      
       
print_r($ceu2);        
       
// however, the elements become sorted by key and lose their original order
           
?>     

</pre>    
    <p class="hide">
    this is a blank paragraph to do nothing but take up space and ensure the formatting works
    </p>
    <div class="stretch"></div>
</body>
</html>