How can PHP arrays be effectively utilized to store and manipulate data from multiple database tables?
When dealing with data from multiple database tables in PHP, arrays can be effectively utilized to store and manipulate this data. By fetching results from each table as an associative array and then combining these arrays into a multidimensional array, you can easily access and work with the data from different tables in a structured manner.
// Fetch data from multiple tables
$table1_results = $pdo->query("SELECT * FROM table1")->fetchAll(PDO::FETCH_ASSOC);
$table2_results = $pdo->query("SELECT * FROM table2")->fetchAll(PDO::FETCH_ASSOC);
// Combine results into a multidimensional array
$combined_data = array(
'table1' => $table1_results,
'table2' => $table2_results
);
// Access and manipulate data from different tables
foreach($combined_data['table1'] as $row) {
echo $row['column_name'];
}