在PHP中,在2 D阵列的行数和列数? [英] In php, Number of rows and columns in a 2 D array?

查看:166
本文介绍了在PHP中,在2 D阵列的行数和列数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数目不详的元素的二维数组。

$ two_darray [行] [列]; //会有一个未知的整数值,而不是行和列的关键字

如果我是如下写循环,我怎么能确定有多少行和列在我的 $ two_darray 。你能告诉我,如果有在PHP中的库函数,可以告诉我里面的值[????] [????]

 为($行= 0; $行< .........; $行++)
{
    为($栏= 0; $柱< .........; $柱++)
    {
        回声$ two_darray [$行] [$列];
    }
    回声\\ n结尾一列的\\ n;
}

我真的需要知道,以便执行其他计算的行和列的值。


解决方案

 的foreach($ two_darray为$关键=> $行){
   的foreach($行为$键2 => $ VAL){
      ...
   }
}

没有必要担心有多少元素是每个阵列中,如的foreach()会照顾它。如果你绝对拒绝使用的foreach,那么就计数()每个阵列,因为它出现了。

  $行=计数($ two_d_array);
为($行= 0; $行< $行; $行++){
     $ COLS =计数($ two_darray [$行]);
     为($ COL = 0; $山坳< $ COLS; $山坳++){
        ...
     }
}

I have a two dimensional array with unknown number of elements.

$two_darray[row][column]; //there will be an unknown integer values instead of row and column keywords

If I were to write a for loop as follows, how can I determine how many rows and columns in my $two_darray. Can you please tell me if there is a library function in php that can tell me the value inside [????] [????]

for($row=0; $row<………; $row++)
{
    for($column =0; $column  <………; $ column ++)
    {
        echo $two_darray[$row][$column];
    }
    echo "\n end of one column \n";
}

I really need to know the value of rows and columns in order to perform other calculations.

解决方案

foreach ($two_darray as $key => $row) {
   foreach ($row as $key2 => $val) {
      ...
   }
}

No need to worry about how many elements are in each array, as foreach() will take care of it for you. If you absolutely refuse to use foreach, then just count() each array as it comes up.

$rows = count($two_d_array);
for ($row = 0; $row < $rows; $row++) {
     $cols = count($two_darray[$row]);
     for($col = 0; $col < $cols; $col++ ) {
        ...
     }
}

这篇关于在PHP中,在2 D阵列的行数和列数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆