What is the difference between arrays and tables in PHP, and how can one implement tabular structures using arrays?
Arrays in PHP are a data structure that can hold multiple values. Tables, on the other hand, are typically used in databases to store data in a structured format. To implement tabular structures using arrays in PHP, you can create an array of arrays where each sub-array represents a row in the table.
// Creating a tabular structure using arrays
$table = array(
array('Name', 'Age', 'City'),
array('John', 25, 'New York'),
array('Jane', 30, 'Los Angeles'),
array('Mike', 22, 'Chicago')
);
// Displaying the table
echo "<table border='1'>";
foreach ($table as $row) {
echo "<tr>";
foreach ($row as $cell) {
echo "<td>$cell</td>";
}
echo "</tr>";
}
echo "</table>";
Keywords
Related Questions
- How can the mysql_data_seek function be used to reset a query in PHP, and what are the correct parameters to pass to it?
- How can I display a column of data and then show the rest of the row data when clicking on a specific entry in PHP?
- What are some best practices for optimizing PHP code to efficiently include content on a webpage without unnecessary reloads?