数组计算操作
16lz
2022-01-22
数组的计算操作(更新中)
- count/sizeof 函数
返回数组的元素个数
$phone = array('苹果','小米','华为','锤子','联想');
echo count($phone); // 5
$price = array(
array('小米8', 2999),
array('苹果11', 50, 7999)
);
echo count($price, 1); // 7;默认0,1则递归多维数组的元素个数
- array_sum 函数
求出数组所有值的和
$num = array(1,2,3,4,5);
echo array_sum($num);
- array_product 函数
求出数组所有值的乘积
$num = array(1,2,3,4,5);
echo array_product($num);
- array_unique 函数
移除数组中的重复值
$phone = array('苹果','小米','华为','苹果','锤子','联想');
$unique = array_unique($phone);
print_r($unique);
- array_flip 函数
将数组的 键值对 对调
$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);
更多相关文章
- 一看就会Android之分组列表组件ExpandableListView的使用
- Android第三课 联调华为手机
- Android开发之手机震动器
- ES6 变量声明,箭头函数,数组方法,解构赋值,JSON,类与继承,模块化练习
- 箭头函数的基础使用
- Android中文API(144) —— JsonWriter
- 华为手机Android(安卓)Studio开发不显示Logcat解决办法
- android sqlite 图片保存和读出
- Android评论留言页面