<html>
<body bgcolor=lightyellow>

<H1 Align=center>Array Functions</h1>

<?php
    $animals = ['pig'=>'oink', 'cow'=>'moo', 'duck'=>'quack', 'dog'=>'woof'];

    print "<b>The original animals array: </b><br>";
    print "<pre>";
    print_r( $animals );
    print "</pre><hr/>";
    
//  Adding an element to the array -------------------------------------------

    $animals['cat'] = 'meow';                    //add another element
    $dogSound = $animals['dog'];                 //retrieve an element

    print "<b>Adding a cat element </b><br><pre>";
    print_r( $animals );
    print "</pre><hr/>";

    $howMany = count($animals);                 //result 4
    print "<b>How many elements:</b> $howMany <br/>";
    print "<hr/>";

//  Changing a value of an element -------------------------------------------

    print "<b>Changing the value of an element: </b><br><pre>";
    $animals['pig'] = '????';
    print_r($animals);
    print "</pre><hr/>";

//  Removing an element from the array --------------------------------------

    print "<b>Removing the pig element: </b><br><pre>";
    unset($animals['pig']);
    print_r($animals);
    print "</pre><hr/>";

//  Reversing the order ------------------------------------------------

    print "<b>Reversing the order of elements:</b><br><pre>";
    $animals = array_reverse($animals, true);        //reverse the order and preserve the key/values 
    print_r($animals);
    print "</pre><hr/>";

//  Getting all the keys ------------------------------------------------

    $keys = array_keys($animals);                   //array keys 
    print "<b>The array keys are:</b><br/>";
    foreach ($keys as $key)
        print "$key, ";
    print "<hr/>";

//  Getting all the values ------------------------------------------------

    $values = array_values($animals);               //array keys 
    print "<b>The array values are:</b><br/>";
    foreach ($values as $value)
        print "$value, ";
    print "<hr/>";

//  Puttng the array in an HTML table ---------------------------------------

    print "<b>The array in an HTML table</b><br/><br/>";
    print "<table border=2 bgcolor=dddddd>";
    print "<tr bgcolor=tan><th>Array Key</th><th>Array Value</th></tr>";

    foreach ($animals as $key => $value)
        print "<tr><th>$key</th><td>$value</td></tr>";

    print "</table>";
    print "<hr/>";

//  Other array functions -------------------------------------------------

    $maximum = max($animals);                           //result woof
    print "<b>The maximum value is:</b> $maximum <br/>";
    print "<hr/>";

    $found = in_array('moo',$animals);                  //result true 
    print "<b>Is 'moo' an element value?</b> $found <br/>";
    print "<hr/>";

    $where = array_search('moo',$animals);              //result cow 
    print "<b>What is the index/key for 'moo'?</b> $where <br/>";
    print "<hr/>";

//  Sorting the array by keys -------------------------------------------------

    print "<b>Sort the array by keys: </b><br><pre>";
    ksort($animals);
    print_r($animals);
    print "</pre><hr/>";

//  Sorting the array by values -------------------------------------------------

    print "<b>Sort the array by values: </b><br><pre>";
    asort($animals);
    print_r($animals);
    print "</pre><hr/>";

?>

<?php include "../include.php"; ?>      <!-- hyperlink to see the code -->
</body>
</html>