A
A
AlexTym2017-04-25 12:04:09
PHP
AlexTym, 2017-04-25 12:04:09

php. How to count separately the number of columns and rows of a two-dimensional array?

There is a filled array.
since the array is small, I can record the number of rows and columns myself. It is interesting to know how to tell the program to count the elements separately for rows and columns (instead of rows, columns in the loop).

$arr =array($ar1=array("рр","ыы","ыаы",20,"ыаы",2),
                   $ar2=array("ыпв","ып","вп",9,"впрр",4),
                   $ar3=array( "вр","вр","вр",30,"вр",5),
                       $ar4=array("врар","ароа","аоао",50,"ааоао",12));


          $rows=4;
          $columns=6;
          for($i=0;$i<$rows;$i++)
          {
            for($j=0;$j<$columns;$j++)
                {

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Burov, 2017-04-25
@AlexTym

$arr = [
        ["рр","ыы","ыаы",20,"ыаы",2],
        ["ыпв","ып","вп",9,"впрр",4],
        [ "вр","вр","вр",30,"вр",5],
        ["врар","ароа","аоао",50,"ааоао",12]
    ];


    for($i = 0; $i < count($arr); $i++)
    {
        for($j = 0; $j < count($arr[$i]); $j++)
        {
        }
    }

A
Alexander Pushkarev, 2017-04-25
@AXP-dev

You can simply calculate the number like this:

$count = 0;
$data = [
    ['a', 'b', 'c'],
    ['d', 'd', 'e'],
    ['f', 'h', 'j']
];

foreach ($data as $datum) {
    $count += count($datum);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question