sizeof函数_PHP sizeof()函数与示例

sizeof函数

PHP sizeof()函数 (PHP sizeof() function)

sizeof() function is an alias of count() function, it is used to get the total number of elements of an array.

sizeof()函数count()函数的别名,用于获取数组元素的总数。

Syntax:

句法:

    sizeof(array, [count_mode])

Here,

这里,

  • array is the name of an input array

    array是输入数组的名称

  • count_mode is an optional parameter and it's default value is 0, it has two values

    count_mode是一个可选参数,默认值为0,它有两个值

    0 – It does not count all elements of a multidimensional array

    0 –不计算多维数组的所有元素

    1 – It counts all elements of a multidimensional array

    1 –计算多维数组的所有元素

Examples:

例子:

    Input:
    $arr1 = array("101", "102", "103", "104", "105");

    Output:
    arr1 has 5 elements

PHP code: Using single dimensional array

PHP代码:使用一维数组

<?php    
    $arr1 = array("101", "102", "103", "104", "105");
    $arr2 = array("Amit", "Abhishek", "Prerana", "Aleesha", "Prem");
	
	$len = sizeof($arr1);
	print ("arr1 has $len elements\n");
	$len = sizeof($arr2);
	print ("arr2 has $len elements\n");	
?>

Output

输出量

arr1 has 5 elements
arr2 has 5 elements

PHP code: Using multidimensional array

PHP代码:使用多维数组

<?php    
    $students = array(
        "101" => array(
                "name" => "Amit",
                "age" => 21,
            ),
        "102" => array(
                "name" => "Abhi",
                "age" => 20,
            )
        );

    $len = sizeof($students);
    print ("sizeof value = $len (sizeof_mode is not provided)\n");

    $len = sizeof($students, 0);
    print ("sizeof value = $len (sizeof_mode is set to 0)\n");    
    
    $len = sizeof($students, 1);
    print ("sizeof value = $len (sizeof_mode is set to 1)\n");    
?>

Output

输出量

sizeof value = 2 (sizeof_mode is not provided)
sizeof value = 2 (sizeof_mode is set to 0)
sizeof value = 6 (sizeof_mode is set to 1)


翻译自: https://www.includehelp.com/php/sizeof-function-with-example.aspx

sizeof函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值