Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How to display values from array of php in html table

take foreach loop inside php directive

The error you're encountering, "Warning: Array to string conversion," is because you're trying to directly echo an array in PHP. In your code, $influencer_name is an array, as shown in your debugging output:

["influencer_name"] => array(4) {
    [0] => string(4) "Amit"
    [1] => string(6) "Roshan"
    [2] => string(6) "Roshan"
    [3] => string(16) "Roshan kumar jha"
}
Enter fullscreen mode Exit fullscreen mode

To display the values from the influencer_name array in your HTML table, you need to iterate through the array and echo each element separately. Here's an example of how you can do that within your HTML table:

<table>
    <tr>
        <td style="font-size:12px;font-weight:bold;text-align:center;">
            <?php foreach ($influencer_name as $name) : ?>
                INR <?php echo $name; ?> /-
            <?php endforeach; ?>
        </td>
    </tr>
</table>
Enter fullscreen mode Exit fullscreen mode

In this code, we use a foreach loop to iterate through the $influencer_name array and echo each element within the table cell. This way, you'll display each name with the appropriate formatting.

Practical Examples

include "DBConnection.php";
if(isset($_POST["admin_id"])){
    echo "Debugging the POST data: ";
    var_dump($_POST);
    foreach ($_POST as $key => $value) {
        if (is_array($value)) {
            // If the value is an array, iterate through its elements
            echo "$key:<br>";
            foreach ($value as $item) {
                echo "- $item<br>";
            }
        } else {
            echo "$key: $value<br>";
        }
    }
Enter fullscreen mode Exit fullscreen mode

output

Image description

step2: now i got array of php using above code

["influencer_name"]=> array(4) { [0]=> string(4) "Amit" [1]=> string(6) "Roshan" [2]=> string(6) "Roshan" [3]=> string(16) "Roshan kumar jha" }

===============or=============
influencer_name:

  • Amit
  • Roshan
  • Roshan
  • Roshan kumar jha

step 3: now i have to display above array of php in table line by line

<td style="font-size:12px;font-weight:bold;text-align:center;">
                                    <?php foreach ($name as $name) : ?>
                                        Name:- <?php echo $name; ?> /-<br>
                                    <?php endforeach; ?>
                                </td>
Enter fullscreen mode Exit fullscreen mode

output

Image description

Top comments (0)