What are the advantages and disadvantages of using arrays versus a database for storing quiz or learning program data in PHP?

When storing quiz or learning program data in PHP, using arrays can be advantageous for smaller datasets due to their simplicity and ease of use. However, for larger datasets or when data needs to be persistently stored and accessed efficiently, using a database like MySQL can be more suitable. Databases offer features such as indexing, querying, and data integrity that arrays may lack.

// Example of storing quiz data using arrays
$quizData = [
    [
        'question' => 'What is the capital of France?',
        'options' => ['London', 'Paris', 'Berlin', 'Madrid'],
        'answer' => 'Paris'
    ],
    [
        'question' => 'What is the largest planet in our solar system?',
        'options' => ['Mars', 'Venus', 'Jupiter', 'Saturn'],
        'answer' => 'Jupiter'
    ]
];

// Example of storing quiz data using a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "quiz_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// SQL to create a table for quiz questions
$sql = "CREATE TABLE quiz_questions (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    question VARCHAR(255) NOT NULL,
    options TEXT NOT NULL,
    answer VARCHAR(255) NOT NULL
)";

if ($conn->query($sql) === TRUE) {
    echo "Table created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

// Close connection
$conn->close();