php 函数的操作
//二维数组 <?php $data = array( array( 'id' => 5698, 'first_name' => 'Bill', 'last_name' => 'Gates', ), array( 'id' => 4767, 'first_name' => 'Steve', 'last_name' => 'Aobs', ), array( 'id' => 3809, 'first_name' => 'Mark', 'last_name' => 'Zuckerberg', ) ); //根据字段last_name对数组$data进行降序排列 $last_names = array_column($data,'last_name'); array_multisort($last_names,SORT_DESC,$data);
- SORT_ASC - 默认。按升序排列 (A-Z)。
- SORT_DESC - 按降序排列 (Z-A)。
var_dump($data);
//对一维数组排序(多个) $ar1 = array(10, 100, 100, 0); $ar2 = array(1, 3, 2, 4); array_multisort($ar1, $ar2); var_dump($ar1); var_dump($ar2); //对一维数组排序(单个) sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据值,以升序对关联数组进行排序 ksort() - 根据键,以升序对关联数组进行排序 arsort() - 根据值,以降序对关联数组进行排序 krsort() - 根据键,以降序对关联数组进行排序
//对数组根据指定值去重
$arr = array( '0'=>array( 'name'=>'james', 'age'=>30, ), '1'=>array( 'name'=>'susu', 'age'=>26, ), '2'=>array( 'name'=>'james', 'age'=>30, ), 'new'=>array( 'name'=>'kube', 'age'=>37, ), 'list'=>array( 'name'=>'kube', 'age'=>27, ), );
//使用方法
$key ='name'; $arr_key = second_array_unique_bykey($arr,$key); printf("As for the givenkey->%s:<br>",$key); print_r($arr_key); echo "<br/>";
//获取数组中所有执行的key
//定义数组
$user_list=array(
'0'=>[
'id'=>1,
'name'=>'刘德华'
],
'1'=>[
'id'=>2,
'name'=>'张学友'
],
'2'=>[
'id'=>3,
'name'=>'黎明'
],
'3'=>[
'id'=>4,
'name'=>'郭富城'
]
);
pp($user_list);
//获取数组中元素id的值,组成一个新数组
$user_id_list=array_column($user_list,'id');
pp($user_id_list);
//将数组用逗号分隔
echo $user_ids=implode(',',$user_id_list);
//对一维数组去重
$arraytest = array('tommy','tommy','abcd');
$arraytest = array_unique($arraytest);
print_r($arraytest);
//获取重复数据
array_diff_assoc(arr,'aaa');
//将数组下标重0开始
array (size=2)
4 =>
array (size=3)
'goods_name' => string '发放是否' (length=12)
'goods_price' => string '1111.00' (length=7)
'goods_nums' => string '111' (length=3)
5 =>
array (size=3)
'goods_name' => string '发放是否' (length=12)
'goods_price' => string '1111.00' (length=7)
'goods_nums' => string '111' (length=3)
使用 array_values($数组) 会把数组的键默认从下标0开始 结果:
array (size=2)
0=>
array (size=3)
'goods_name' => string '发放是否' (length=12)
'goods_price' => string '1111.00' (length=7)
'goods_nums' => string '111' (length=3)
1=>
array (size=3)
'goods_name' => string '发放是否' (length=12)
'goods_price' => string '1111.00' (length=7)
'goods_nums' => string '111' (length=3)
//让数组倒叙输出(从后向前输出)
array_reverse($list,true);
//获取数组中指定的值
$arr = array_column($plan,'id');
//获取数组中指定值得下标
array_search('2',$arr)
//获取数组中数值的总和
$a=array(0=>"5",1=>"15",2=>"25");
echo array_sum($a);
//正负数翻转
$number = 99;
$number *= -1;
echo $number; //输出 -99
$number = -11;
$result = abs($number);
echo $result; //输出 11
//数组合并
$a = array(array("1","2"),array("3","4"));
$b = array(array("a","b"),array("c","d"));
$c = array_merge($a,$b);
print_r($c);
//去除字符串中所有的空格
echo str_replace(' ', '', 'ab ab');
//输出 "abab'
//去除字符串中两端的空格
trim();
//去除字符串换行
//使用PHP系统常量【推荐】 $str = str_replace(PHP_EOL, '', $str);
//快速删除数组中指定的值
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result); //Array ( [d] => yellow )
//将一位数组转二维数组
$stack = array(1,2,3,4,5,6,7,8);
$split = 4;
$array = array_chunk($stack, $split);
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) [1] => Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 ) )
//去除数组中的空值array_filter
(
$array
)
//把字符串根据位数分割
array_fill()
//打印所占内存
var_dump(memory_get_usage());
//获取二维数组中某个字段值等于x的个数
$array = [
['field' => 1],
['field' => 2],
['field' => 1],
['field' => 3],
['field' => 1],
];
$fieldName = 'field';
$valueToCount = 1;
$count = count(array_filter($array, function ($item) use ($fieldName, $valueToCount) {
return isset($item[$fieldName]) && $item[$fieldName] == $valueToCount;
}));
echo $count; // 输出等于1的个数
?>