Sai A Sai A
Updated date Feb 22, 2024
In this blog, we will learn how to convert PHP arrays into HTML tables with customized CSS classes for styling, using simple methods.

Introduction:

HTML tables are fundamental elements for presenting data on web pages. In PHP, you might often find yourself needing to display array data in a tabular format. However, manually creating HTML tables from arrays can be tedious and time-consuming. Fortunately, PHP offers straightforward methods to automate this process efficiently. In this blog, we will explore multiple approaches to convert PHP arrays into HTML tables.

Method 1: Using Basic HTML Table Structure

We will start with the most basic method, which involves manually constructing the HTML table structure using loops to iterate over the array elements and outputting them within the table tags. Let's dive into the code:

<?php
// Sample array data
$data = array(
    array('Name', 'Age', 'Country'),
    array('John', 25, 'USA'),
    array('Alice', 30, 'UK'),
    array('Bob', 28, 'Canada')
);

// Output HTML table
echo '<table class="custom-table">';
foreach ($data as $row) {
    echo '<tr>';
    foreach ($row as $cell) {
        echo '<td>' . $cell . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>

Output:

<table class="custom-table">
    <tr><td>Name</td><td>Age</td><td>Country</td></tr>
    <tr><td>John</td><td>25</td><td>USA</td></tr>
    <tr><td>Alice</td><td>30</td><td>UK</td></tr>
    <tr><td>Bob</td><td>28</td><td>Canada</td></tr>
</table>

This method involves manually iterating over the array elements using nested loops. We output <table> tags to start the table and <tr> tags for each row. Within each row, we output <td> tags for each cell in the array. The resulting HTML table can be styled using the "custom-table" class.

Method 2: Using HTML Table Builder Function

For a more structured and reusable approach, we can define a PHP function to generate the HTML table from the array. This method enhances code readability and maintainability. Let's see how it's done:

<?php
// Function to generate HTML table
function generate_table($data, $class) {
    $html = '<table class="' . $class . '">';
    foreach ($data as $row) {
        $html .= '<tr>';
        foreach ($row as $cell) {
            $html .= '<td>' . $cell . '</td>';
        }
        $html .= '</tr>';
    }
    $html .= '</table>';
    return $html;
}

// Sample array data
$data = array(
    array('Name', 'Age', 'Country'),
    array('John', 25, 'USA'),
    array('Alice', 30, 'UK'),
    array('Bob', 28, 'Canada')
);

// Output HTML table
echo generate_table($data, 'custom-table');
?>

Output:

<table class="custom-table">
    <tr><td>Name</td><td>Age</td><td>Country</td></tr>
    <tr><td>John</td><td>25</td><td>USA</td></tr>
    <tr><td>Alice</td><td>30</td><td>UK</td></tr>
    <tr><td>Bob</td><td>28</td><td>Canada</td></tr>
</table>

In this method, we encapsulate the table generation logic into a reusable function generate_table(). This function accepts the array data and the desired CSS class for the table as arguments. It then iterates over the array to construct the table HTML dynamically. This approach promotes code reusability and modularity.

Method 3: Using PHP's array_map() Function

Another elegant way to generate HTML tables from arrays is by utilizing PHP's array_map() function along with the implode() function to concatenate array elements. This method simplifies the code further. Let's see it in action:

<?php
// Sample array data
$data = array(
    array('Name', 'Age', 'Country'),
    array('John', 25, 'USA'),
    array('Alice', 30, 'UK'),
    array('Bob', 28, 'Canada')
);

// Function to generate HTML table rows
function create_table_row($row) {
    return '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
}

// Output HTML table
echo '<table class="custom-table">';
echo implode('', array_map('create_table_row', $data));
echo '</table>';
?>

Output:

<table class="custom-table">
    <tr><td>Name</td><td>Age</td><td>Country</td></tr>
    <tr><td>John</td><td>25</td><td>USA</td></tr>
    <tr><td>Alice</td><td>30</td><td>UK</td></tr>
    <tr><td>Bob</td><td>28</td><td>Canada</td></tr>
</table>

In this method, we define a helper function create_table_row() that takes a row from the array and constructs the HTML <tr> tag with <td> tags for each cell using implode(). Then, we use array_map() to apply this function to each row of the array, generating an array of HTML table rows. Finally, we concatenate these rows to form the complete HTML table.

Conclusion:

In this blog, we have explored various methods for converting PHP arrays into HTML tables with custom classes for styling. From the basic manual approach to more advanced techniques using functions and PHP array functions, each method offers its advantages in terms of readability, maintainability, and flexibility. 

Comments (0)

There are no comments. Be the first to comment!!!