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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,104 questions

40,777 answers

573 users

How to declare, initialize and print two-dimensional (2D) array of integers in PHP

3 Answers

0 votes
function print_array($arr2d, $size)
{
    echo '<table border="0" cellspacing="3">';
    for ($i = 0; $i < $size; $i++)
    {
        echo "<tr align='right'>";
        for ($j = 0; $j < $size; $j++)
            echo "<td>" . $arr2d[$i][$j] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
   
$a = array(array(1, 8, 5),
           array(6, 7, 1),
           array(8, 7, 6));
$size = 3;
   
print_array($a, $size);

/*
run:
 
1 8 5 
6 7 1 
8 7 6 
 
*/

 





answered Mar 1, 2016 by avibootz
edited Nov 15, 2016 by avibootz
0 votes
function print_array($arr2d)
{
    echo '<table border="0" cellspacing="3">';
    $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>";
}
   
$a = array(array(1, 8, 5),
           array(6, 7, 1));
 
print_array($a);
  
      
/*
run:
  
1 8 5 
6 7 1 
       
*/

 





answered Mar 1, 2016 by avibootz
edited Nov 15, 2016 by avibootz
0 votes
function print_array($arr2d)
{
    echo '<table border="0" cellspacing="3">';
    $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>";
}
  
$a = [[]];
  
for ($i = 0; $i < 2; $i++)
    for ($j = 0; $j < 3; $j++)
        $a[$i][$j] = rand(2, 9);

print_array($a);
 
     
/*
run:
 
5    2    2
8    7    8
      
*/

 





answered Mar 1, 2016 by avibootz
...