PHP Lesson 7 array usage 2. PHP Lesson 7 array usage 2 Learning outline: 1. understanding array functions 2. random output verification code 1. array function: Array Function: provides a lot of useful code segments officially written. This article introduces the usage of PHP class 7 array 2.
Learning outline:
1. understand array functions
2. random output verification code
1. array functions:
Array functions:
// Function: provides a lot of useful code segments officially written to improve the writing speed.
1. array key-value operation functions
2. calculate the element and uniqueness of the array.
3. use the callback function to process array functions
4. array sorting function
5. split, merge, decompose and combine functions
6. array and data structure
7. other useful array processing functions
Array key-value operation functions:
1. array_values ();
Obtains the key and value values.
"user1","age"=>"30","sex"=>"man");foreach($arr as $key=>$val){$keys[]=$key;$vals[]=$val;}echo "";print_r($keys);echo "
";echo "";echo "";print_r($vals);echo "
";?>
2. use of array_values
"user1","age"=>"30","sex"=>"man");$keys=array_values($arr);echo "";print_r($keys);echo "
";?>
Array_values (); // obtain the value in the array
Array_keys (); // Obtain 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 (); // key and value pairs
Array_reverse (); reverse the value in the array
Measure the element and uniqueness of an array.
1. count ();
2. array_count_values (); // counts the number of occurrences of each value in the array.
3. array_unique (); // delete duplicates in the array
Use the callback function to process array functions:
1. array_filter ();
70,60,80,78,34,34,34,56,78,78);function older($var){return ($var>60);}$arr2=array_filter($arr,"older");echo "";print_r($arr2);echo "
";?>
2. array_map ();
Reference parameters:
Requirement: Array Value auto-Increment 1
Function show (& $ arr ){
Foreach ($ arr as $ key => $ val ){
$ Arr [$ key] = $ val + 1;
}
}
Array sorting function
1. sort (); in ascending order, keys are not retained
2. rsort (); in descending order, keys are not retained
3. asort (); in ascending order, the key is retained.
4. arsort (); in descending order, the key is retained.
5. ksort (); sort by key in ascending order
6. krsort (); sort by key in descending order
7. natsort (); Natural numbers are sorted in ascending order, compared to img2.jpg
8. natcasesort (); ignore case-insensitive ascending order
9. multisort (); Multi-array sorting
Ksort ();
10,"b"=>1,"c"=>3,"d"=>30);$arr2=array_flip($arr);ksort($arr2);echo "";print_r($arr2);echo "
";?>
Natsort ();
Multi-array sorting:
"; Print_r ($ arr2); echo"
";?>
Splitting, merging, splitting, and combining functions
1. explode ();
2. inplode (); // join ();
3. array_slice (); array truncation
4. array_splice (); array pruning
5. array-merge (); merge multiple arrays
6. array_combine (); merges two arrays. the first array is the key, and the last array is the value.
7. array_intersect (); returns the intersection of two arrays.
8. array_diff (); find the differences between the two arrays, according to the first parameter
9. array_pop (); the last pop-up value is returned.
10. array_push (); press a value from the last position to return the number of elements.
11. array_shift (); delete a value from the front of the wash.
12. array_unshift (); press a value from the beginning
";print_r($arr);echo "
";?>
2. inplode (); combines the array into a string
";print_r($str2);echo "
";?>
"; Print_r ($ str2); echo"
";?>
Array_slice ();
"; Print_r ($ arr3); echo"
";?>
Not only can be removed, but can also be added
"; Print_r ($ arr2); echo"
"; Echo"
"; print_r($arr); echo "
";?> Array_merge (); "; Print_r ($ arr); echo"";?>
Other useful array processing functions:
1. array_rand (); // random key
2. range (); // retrieves an array of a certain range
3. shuffle (); // disrupt the functions of the array
4. array_sum (); // calculate the sum of all people in the array (calculate the total score)
If you calculate the sum of keys of an array, you can use array_flip () to check the health and value of the array, and then calculate the sum of keys.
"; Print_r ($ arr2); echo"
";?>
// Outputs a four-character verification code at random:
Reprinted please indicate the source: http://blog.csdn.net/junzaivip
Usage of arrays 2 Learning outline: 1. understanding array functions 2. random output verification code 1. array function: Array Function: // Function: provides many useful code segments officially written...