Skip to content
PHP Logo

What are the differences between numerically indexed arrays and associative arrays in PHP, and how can they impact table generation?

Numerically indexed arrays in PHP use integer keys to access values in the array, while associative arrays use strings as keys. When generating tables, numerically indexed arrays are useful for maintaining a sequential order of data, while associative arrays are helpful for associating data with specific labels or keys. Depending on the structure of the data, choosing the appropriate array type can impact the ease of generating tables with the desired layout and content.

php
// Numerically indexed array for table generation
$numericalArray = array("John", "Doe", 30);
echo "<table>";
echo "<tr>";
foreach ($numericalArray as $value) {
    echo "<td>$value</td>";
}
echo "</tr>";
echo "</table>";

// Associative array for table generation
$associativeArray = array("name" => "John", "surname" => "Doe", "age" => 30);
echo "<table>";
echo "<tr>";
foreach ($associativeArray as $key => $value) {
    echo "<td>$key: $value</td>";
}
echo "</tr>";
echo "</table>";