How can a 3-dimensional array be created and manipulated in PHP when extracting data from a MySQL database?

When extracting data from a MySQL database in PHP, you can create a 3-dimensional array by using nested loops to iterate over the result set. Each row from the database can be stored as an array, and then pushed into another array representing a table or collection of data. This allows for easy manipulation and organization of data in a multi-dimensional structure.

// Connect to MySQL database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

// Query to fetch data from database
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Create a 3-dimensional array to store the data
$data = array();

// Loop through the result set and store each row as an array
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

// Manipulate the 3-dimensional array as needed
foreach ($data as $table) {
    foreach ($table as $row) {
        // Access individual data elements
        echo $row['column_name'];
    }
}

// Close the database connection
mysqli_close($connection);