How can array keys be dynamically set in PHP based on table names and field names from a query result?
To dynamically set array keys in PHP based on table names and field names from a query result, you can loop through the query result and use the table name as the key for the main array, and the field names as keys for the nested arrays. This way, you can organize the data in a structured manner that reflects the database schema.
// Assuming $queryResult is the result of a database query
$data = [];
while ($row = $queryResult->fetch_assoc()) {
foreach ($row as $key => $value) {
$tableName = 'table_name'; // Replace with actual table name
$data[$tableName][$key] = $value;
}
}
print_r($data);