数组计算操作
发布于:2021-12-20 10:34:06
次阅读
数组的计算操作(更新中)
$phone = array('苹果','小米','华为','锤子','联想');echo count($phone); // 5
$price = array( array('小米8', 2999), array('苹果11', 50, 7999));echo count($price, 1); // 7;默认0,1则递归多维数组的元素个数
$num = array(1,2,3,4,5);echo array_sum($num);
- array_product 函数求出数组所有值的乘积
$num = array(1,2,3,4,5);echo array_product($num);
$phone = array('苹果','小米','华为','苹果','锤子','联想');$unique = array_unique($phone);print_r($unique);
$computer = array( '联想'=>'Y900P', '神州'=>'Z8', '苹果'=>'Sierra',);$flip = array_flip($computer);print_r($flip);
- array_rand 函数从数组中随机取出一个或多个元素的索引
$phone = array('苹果','小米','华为','锤子','联想');$rand = array_rand($phone); // 随机返回一个索引echo $rand;
$phone = array('苹果','小米','华为','锤子','联想');$rand = array_rand($phone,3); // 以数组的形式随机print_r($rand);

- array_count_values 函数数组形式返回每个元素值出现的次数
$phone = array('苹果','小米','华为','锤子','苹果','联想');$repeat = array_count_values($phone);print_r($repeat);
