What are some best practices for transforming a list of data records into a cross-table format using PHP?
Transforming a list of data records into a cross-table format involves pivoting the data so that rows become columns. This can be achieved by looping through the data records and organizing them into an array where each column represents a unique value from the original data. This array can then be used to generate a cross-table format for easier analysis and presentation.
// Sample list of data records
$data = [
['id' => 1, 'category' => 'A', 'value' => 10],
['id' => 2, 'category' => 'B', 'value' => 20],
['id' => 3, 'category' => 'A', 'value' => 30],
['id' => 4, 'category' => 'B', 'value' => 40],
];
// Initialize an empty array to store cross-table data
$crossTable = [];
// Loop through the data records and organize them into the cross-table array
foreach ($data as $record) {
$crossTable[$record['category']][] = $record['value'];
}
// Output the cross-table data
print_r($crossTable);