Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to display 2D array as HTML table in PHP

1 Answer

0 votes
function print_array($arr2d) {
    echo '<table border="1">';
    $rows = count($arr2d);
    for ($i = 0; $i < $rows; $i++) {
        echo "<tr align='right'>";
        $cols = count($arr2d[$i]);
        for ($j = 0; $j < $cols; $j++)
            echo "<td>" . $arr2d[$i][$j] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
    
$arr = array(array(1, 8, 5, 9),
             array(6, 7, 1, 5),
             array(8, 0, 2, 7, 3),
);
    
       
print_array($arr);
 
   
       
/*
run:
   
1   8   5   9
6   7   1   5
8   0   2   7   3
        
*/

 



answered Aug 1, 2020 by avibootz
edited Aug 1, 2020 by avibootz

Related questions

1 answer 198 views
1 answer 191 views
1 answer 222 views
...