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,859 questions

51,780 answers

573 users

How to find the length (rows, cols) of 2D array in PHP

1 Answer

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, 9),
           array(6, 7, 1, 5),
);
  
echo "total rows = " . count($a) . "<br />";    
for ($i = 0; $i < count($a); $i++)    
     echo "row = " . $i . " cols = " . count($a[$i]) . "<br />";   
     
print_array($a);
 
     
/*
run:
 
total rows = 2
row = 0 cols = 4
row = 1 cols = 4
1    8    5    9
6    7    1    5
      
*/

 



answered Mar 1, 2016 by avibootz

Related questions

1 answer 290 views
4 answers 379 views
4 answers 390 views
2 answers 254 views
1 answer 230 views
...