How can the structure of an array be optimized for efficiency when retrieving data from a database in PHP?

When retrieving data from a database in PHP, the structure of the array can be optimized for efficiency by using associative arrays where the keys are column names. This allows for easy and direct access to specific data without having to iterate through the entire array. By organizing the data in this way, it improves readability and performance when accessing database records.

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

// Create an associative array for storing data
$data = array();

while ($row = mysqli_fetch_assoc($result)) {
    // Store data in associative array with column names as keys
    $data[$row['column1']] = $row;
}

// Access specific data directly from the associative array
echo $data['column1']['column2'];