-
-
$arr=array("name"=>"user1","age"=>"30","sex"=>"man") ;
- foreach($arr as $key=>$val){
- $keys[]=$key;
- $vals[]=$val;
- }
- echo "
"; </li>
<li>print_r($keys ); </li>
<li>echo " ";
- echo "
";
- echo "
"; </li>
<li>print_r($vals); </li>
<li>echo " "; p>
?>
-
Copy code
2.Usage of array_values
-
- $arr=array("name"=>"user1","age"=>"30","sex"=>"man");
- $keys =array_values($arr);
- echo "
"; </li>
<li>print_r($keys); </li>
<li>echo " ";
- ?>
-
Copy code
array_values(); //Get the value in the array
array_keys();//Get the keys in the array
in_array();//Check whether a value is in the array
array_key_exists();//Check whether a key is in the array
array_flip();//Swapping keys and values
array_reverse();Reverse the values in the array
Count the elements and uniqueness of arrays
1.count();
2.array_count_values();//Count the number of occurrences of each value in the array.
3.array_unique();//Delete duplicates in the array
Functions that use callback functions to process arrays:
1.array_filter();
-
- $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 "
"; </li>
<li>print_r($arr2); </li>
<li>echo " ";
- ?>
Copy code
2.array_map();
Reference parameters:
Requirement: Array value increases by 1
-
- function show(&$arr){
- foreach($arr as $key=>$val){
- $arr[$key]=$val+1;
- }
- }
-
Copy code
Sorting function of array
1.sort(); ascending order, key is not retained
2.rsort(); Descending order, key is not retained
3.asort(); ascending order, retain key
4.arsort(); Descending order, keep key
5.ksort(); Sort according to key in ascending order
6.krsort(); Sort by key in descending order
7.natsort(); natural number sorting in ascending order, such as the picture img2.jpg
8.natcasesort(); ignore case and sort in ascending order
9.multisort();Multiple array sorting
ksort();
-
- $arr=array("user1"=>10,"b"=>1,"c"=>3,"d"=>30);
- $arr2=array_flip($arr);
- ksort($arr2);
- echo "
"; </li>
<li>print_r($arr2); </li>
<li>echo " ";
- ?>
Copy Code
natsort();
-
- $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");
- sort($array1) ;
- echo "Standard sortingn";
- print_r($array1);
- natsort($array2);
- echo "nNatural order sortingn";
- print_r($array2);
- ?>
Copy code
Most Group sorting:
-
-
- $arr=array("aaa","bbbbbbbbb","cc","ddddd");
- //Requirements:
- //1. Sort by title length
- // 2. The title length becomes the key of the title string
- //Get the length of the value in the array and use it as a new array
- //strlen($val) to get the length of the string
- foreach ($arr as $val) {
- $lens[]=strlen($val);
- }
- array_multisort($lens,SORT_ASC,$arr);//Sort the array, sort the second array according to the first array SORT_ASC means ascending order
- sort($lens);
- $arr2=array_combine($lens, $arr);//The first array serves as the key corresponding to the second array, returning a new array
- echo "
"; </li>
<li>print_r( $arr2); </li>
<li>echo " ";
- ?>
-
Copy code
Split, merge, decompose and combine functions
1.explode();
2.inplode();//join();
3.array_slice(); Array interception
4.array_splice(); Array cutting
5.array-merge(); merge multiple arrays
6.array_combine(); merge arrays, two arrays, the former array as key, the latter array as value
7.array_intersect(); Find the intersection of two arrays
8.array_diff(); Find the difference between two arrays, based on the first parameter
9.array_pop(); pops a value from the end and returns the pop-up value
10.array_push(); Push a value from the last position and return the number of elements
11.array_shift(); delete a value from the previous position
12.array_unshift(); Push a value from the front position
-
-
- $str="php,js,html,ces,div";
- $arr=explode(",",$str);
- echo "
"; </li>
<li> print_r($arr); </li>
<li>echo " ";
- ?>
Copy code
2.inplode(); Combine arrays into strings
-
-
- $str="php,js,html,ces,div";
- $arr=explode(",",$str);
- $str2=implode ("-",$arr);
- echo "
"; </li>
<li>print_r($str2); </li>
<li>echo " ";
- ?>
< ;?php
- $str="php,js,html,ces,div";
- $arr=explode(",",$str);
- $arr2=array_reverse($arr);//Talk about the values in the array Perform reverse order
- $str2=implode("-",$arr2);
- echo "
"; </li>
<li>print_r($str2); </li>
<li>echo " ";
- ?>
- p>
-
Copy code
array_slice();
-
-
- //Interception is always taken from back to front
- $arr = array("aa","bb","cc","dd","ee","ff" ,"gg");
- $arr2 = array_slice($arr, 0,2);//Indicates that 2 aa bbs are intercepted from the 0 position
- $arr3 = array_slice($arr, -3,2);//Indicates Count from the back to the position of 3, start to intercept 2 //ee ff
- echo "
"; </li>
<li>print_r($arr3); </li>
<li>echo " ";
- ?>
Copy the code
Not only can it be removed and subtracted, but it can also be added
-
-
- $arr = array("aa","bb","cc","dd","ee","ff","gg");
- $arr2 = array_splice ($arr, 0, 3, array("hh","ii","jj","kk"));//Directly take the value of the original array and change the original array. The original array will be the value after removal The remaining values
- echo "
"; </li>
<li>print_r($arr2); </li>
<li>echo " ";
- echo "
"; </li>
<li>print_r($arr); </li>
<li>echo "< ;/pre>"; </li>
<li>?></li>
</ol></div>
<em onclick="copycode($('code_bSa'));">Copy code</em>
</div>
<p>array_merge();
</p>
<div class="blockcode">
<div id="code_kMp"><ol>
<li>
<li><?php </li>
<li>$a = array("aa","bb","cc"); </li>
<li>$b = array("dd","ee","ff","gg" ); </li>
<li>$arr = array_merge($a, $b); </li>
<li>echo "<pre class="brush:php;toolbar:false">"; </li>
<li>print_r($arr); </li>
<li>echo " ";
- ?>
-
Copy Code
Other useful array processing functions:
1.array_rand();//Randomly pick a key
2.range();//Get an array of a certain range
3.shuffle();//The function of disrupting the array
4.array_sum();//Calculate the sum of all people in the array (calculate the total score)
If you calculate the key sum of an array, you can use array_flip() to swap the key sum values of the array, and then calculate the key sum.
-
-
- $arr = array("aa","bb","cc","dd","ee","ff","gg");
- //Randomly shuffle the order of the original array
- shuffle($arr);
- //Get the first 3 items of the array
- $arr2= array_slice($arr, 0, 3);
- echo "
"; </li>
<li> print_r($arr2); </li>
<li>echo " ";
- ?>
- //Randomly output four-character verification code implementation:
//Take out 1-9 a-z A-Z array - $a = range(1, 9);
- $b = range(a, z);
- $c = range(A, Z);
- //Combine 3 arrays
- $ d = array_merge($a,$b,$c);
- //Shuffle the merged array
- shuffle($d);
- //Get the first 4 digits after the merge
- $e = array_slice($d, 0, 4);
- //Convert the $e array into a string
- $f = join("", $e);
- echo $f;
- ?>
-
Copy code
|