PHP第七课 数组的用法2
PHP第七课 数组的用法2
学习纲要:
1.了解数组函数
2.随机输出验证码
1.数组函数:
数组函数:
//作用:提供了很多官方写的非常有用的代码段,提高编写速度.
1.数组的键值操作函数
2.统计数组的元素和唯一性
3.使用回调函数处理数组的函数
4.数组的排序函数
5.拆分,合并,分解与结合函数
6.数组与数据结构
7.其他有用的数组处理函数
数组的键值操作函数:
1.array_values();
模拟获取key和value的值
<?php $arr=array("name"=>"user1","age"=>"30","sex"=>"man"); foreach($arr as $key=>$val){ $keys[]=$key; $vals[]=$val; } echo "<pre class="code">"; print_r($keys); echo "
"; echo "
"; print_r($vals); echo "
2.array_values的使用
<?php $arr=array("name"=>"user1","age"=>"30","sex"=>"man"); $keys=array_values($arr); echo "<pre class="code">"; print_r($keys); echo "
array_values();//获取数组中的值
array_keys();//获取数组中的健
in_array();//检查一个值是否在数组中
array_key_exists();//检查一个键是否在数组中
array_flip();//键和值对调
array_reverse();数组中的值反转
统计数组的元素和唯一性
1.count();
2.array_count_values();//统计数组中每个值出现的次数.
3.array_unique();//删除数组中的重复
使用回调函数处理数组的函数:
1.array_filter();
<?php $arr=array("user1"=>70,60,80,78,34,34,34,56,78,78); function older($var){ return ($var>60); } $arr2=array_filter($arr,"older"); echo "<pre class="code">"; print_r($arr2); echo "
2.array_map();
引用参数:
需求:数组值自加1
function show(&$arr){
foreach($arr as $key=>$val){
$arr[$key]=$val+1;
}
}
数组的排序函数
1.sort(); 升序,不保留key
2.rsort(); 降序 ,不保留key
3.asort(); 升序,保留key
4.arsort(); 降序,保留key
5.ksort();根据key排序 升序
6.krsort();根据key排序 降序
7.natsort();自然数排序 升序,比如图片img2.jpg
8.natcasesort();忽略大小写 升序排列
9.multisort();多数组排序
ksort();
<?php $arr=array("user1"=>10,"b"=>1,"c"=>3,"d"=>30); $arr2=array_flip($arr); ksort($arr2); echo "<pre class="code">"; print_r($arr2); echo "
natsort();
<?php $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png"); sort($array1); echo "Standard sorting\n"; print_r($array1); natsort($array2); echo "\nNatural order sorting\n"; print_r($array2); ?>
多数组排序:
<?php $arr=array("aaa","bbbbbbbbb","cc","ddddd"); //需求: //1.按照标题长度排序 //2.标题长度变成标题字符串的key //将数组中的value的长度取出,并作为一个新数组 //strlen($val)取出字符串的长度 foreach ($arr as $val) { $lens[]=strlen($val); } array_multisort($lens,SORT_ASC,$arr);//对数组进行排序,根据第一个数组来排序第二个数组 SORT_ASC表示升序排序 sort($lens); $arr2=array_combine($lens, $arr);//第一个数组作为第二个数组对应的key,返回一个新数组 echo "<pre class="code">"; print_r($arr2); echo "
拆分,合并,分解与结合函数
1.explode();
2.inplode();//join();
3.array_slice();数组的截取
4.array_splice();数组的裁剪
5.array-merge();合并多数组
6.array_combine();合并数组,两个数组,前一个数组作为key,后一个数组作为value
7.array_intersect();找出两个数组的交集
8.array_diff();找出两个数组的不同,根据第一个参数
9.array_pop();从最后弹出一个值,返回弹出值
10.array_push();从最后的位置压入一个值,返回元素的个数
11.array_shift();从洗前面的位置删除一个值
12.array_unshift();从最前的位置压入一个值
<?php $str="php,js,html,ces,div"; $arr=explode(",",$str); echo "<pre class="code">"; print_r($arr); echo "
<?php $str="php,js,html,ces,div"; $arr=explode(",",$str); $str2=implode("-",$arr); echo "<pre class="code">"; print_r($str2); echo "
<?php $str="php,js,html,ces,div"; $arr=explode(",",$str); $arr2=array_reverse($arr);//讲数组中的值进行倒序 $str2=implode("-",$arr2); echo "<pre class="code">"; print_r($str2); echo "
array_slice();
<?php //截取总是从后往前截取 $arr = array("aa","bb","cc","dd","ee","ff","gg"); $arr2 = array_slice($arr, 0,2);//表示从0的位置截取2个 aa bb $arr3 = array_slice($arr, -3,2);//表示从后往前数到3的位置,开始截取2个//ee ff echo "<pre class="code">"; print_r($arr3); echo "
不仅拆减,而且可以添加
<?php $arr = array("aa","bb","cc","dd","ee","ff","gg"); $arr2 = array_splice($arr, 0, 3, array("hh","ii","jj","kk"));//直接取原数组的值,并将原数组进行改变,原数组为取走以后剩下的值 echo "<pre class="code">"; print_r($arr2); echo "
"; print_r($arr); echo "
"; print_r($arr); echo "
其他有用的数组处理函数:
1.array_rand();//随机取一个key
2.range();//取出某个范围的数组
3.shuffle();//打乱数组的作用
4.array_sum();//计算数组内所有人的和(计算总得分)
如果计算数组的key之和,可以采用array_flip()对数组的健和值进行对调,然后就可以算出健之和.
<?php $arr = array("aa","bb","cc","dd","ee","ff","gg"); //将原数组顺序随机打乱 shuffle($arr); //取出数组的前3个 $arr2= array_slice($arr, 0 , 3); echo "<pre class="code">"; print_r($arr2); echo "
//随机输出四位字符 验证码实现:
<?php //取出1-9 a-z A-Z的数组 $a = range(1, 9); $b = range(a, z); $c = range(A, Z); //将3个数组合并 $d = array_merge($a,$b,$c); //将合并后的数组打乱 shuffle($d); //取合并后的前4位 $e = array_slice($d, 0, 4); //将$e数组变为字符串 $f = join("", $e); echo $f; ?>
转载请注明出处: http://blog.csdn.net/junzaivip

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

The PHP array merging and deduplication algorithm provides a parallel solution, dividing the original array into small blocks for parallel processing, and the main process merges the results of the blocks to deduplicate. Algorithmic steps: Split the original array into equally allocated small blocks. Process each block for deduplication in parallel. Merge block results and deduplicate again.
