0712-2888027 189-8648-0214
微信公众号

孝感风信网络科技有限公司微信公众号

当前位置:主页 > 技术支持 > PHP > php中获得数组长度的方法

php中获得数组长度的方法

时间:2024-04-24来源:风信官网 点击: 440次
count — 计算数组中的单元数目或对象中的属性个数
strlen是统计数组中元素的长度;

你如果想统计数组中所有元素的长度,那就用循环统计吧

代码:
$a  =  array('0'  =>  '123',  '1'  =>  'asdf');

  $count  =  count($a);

  $a_length  =  0;

  for  ($i=0;  $i<$count;  $i++)  {
  $a_length  =  $a_length  +  strlen($a[$i]);
  }
  print  $a_length;
 


  代码:
<?phptqeb
$a[0]
?>

Example #1 count() 例子
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3

$b[0]  = 7;
$b[5]  = 9;
$b[10] = 11;
$result = count($b);
// $result == 3

$result = count(null);
// $result == 0

$result = count(false);
// $result == 1
?>

Example #2 递归 count() 例子

<?php
$food = array('fruits' => array('orange', 'banana', 'apple'),
              'veggie' => array('carrot', 'collard', 'pea'));

// recursive count
echo count($food, COUNT_RECURSIVE); // output 8

// normal count
echo count($food); // output 2

?>
热门关键词: php 数组长度
栏目列表
推荐内容
热点内容
展开