example.php

<!DOCTYPE html>
<html>
 
 <head>
  <title>PHP Example</title>
 </head>
 <body>
<?php 

    echo "<h1>Arrays and Functions</h1>";

    $trivia = [];

    $trivia[1] = "How much does the Chewbacca costume weigh?";
    array_push($trivia, "What is the ultimate answer to life, the universe, and everything?");
    $trivia[3] ="How many licks does it take to get to the center of a tootsie roll pop?";

    // Sorting
    sort($trivia); // Sort values, replacing keys
    rsort($trivia); // Reverse sort values, replacing keys
    asort($trivia); // Sort values, keeping keys
    arsort($trivia); // Reverse sort values, keeping keys
    ksort($trivia); // Sort keys
    krsort($trivia); // Reverse sort keys

    echo "<pre>";
    print_r($trivia);
    echo "</pre>";
    
    foreach ($trivia as $v) {
        echo "Qn: $v<br>";
    }

    // check if key exists in the array
    echo array_key_exists(3, $trivia);
    echo isset($trivia[3]);

    // we can also define an array in structure
    $triviaPoints = [
        "100" => "Question",
        "200" => "Question" //...
    ];


    $triviaGame = [
        100 => [
            "question" => "What is the ultimate answer to life, the universe, and everything?",
            "answer" => 42
        ],
        200 => [
            "question" => "How much does the Chewbacca costume weigh?",
            "answer" => "8 pounds"
        ],
        500 => [
            "question" => "How many licks does it take to get to the center of a tootsie roll pop?",
            "answer" => "3"
        ]
    ];

    echo "<pre>";
    print_r($triviaGame);
    echo "</pre>";

    foreach ($triviaGame as $k => $v) {
        echo "$k pt Qn: {$v["question"]}<br>";
    }

    $qn = array_rand($triviaGame);
    echo "Random Question ($qn pts): {$triviaGame[$qn]["question"]}<br>";
?>



 </body>
</html>