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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,401 questions

56,365 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
...