Skip to content
PHP Logo

What are the best practices for processing database outputs in PHP and inserting them into a new two-dimensional array?

When processing database outputs in PHP and inserting them into a new two-dimensional array, it is important to iterate over the results and store them in the desired format. One common approach is to use a loop to fetch each row from the database result set and then append it to the two-dimensional array. Additionally, it is crucial to properly handle any potential errors or exceptions that may occur during the processing.

php
// Assuming $dbResult contains the database output
$twoDimensionalArray = [];

while ($row = $dbResult->fetch_assoc()) {
    $twoDimensionalArray[] = $row;
}

// Now $twoDimensionalArray contains the database outputs in a two-dimensional array format