PHP 7 array usage 2, php array usage
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.
<?php$arr=array("name"=>"user1","age"=>"30","sex"=>"man");foreach($arr as $key=>$val){$keys[]=$key;$vals[]=$val;}echo "<pre>";print_r($keys);echo "</pre>";echo "
2. Use of array_values
<?php$arr=array("name"=>"user1","age"=>"30","sex"=>"man");$keys=array_values($arr);echo "<pre>";print_r($keys);echo "</pre>";?>
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 ();
<?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>";print_r($arr2);echo "</pre>";?>
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 ();
<?php$arr=array("user1"=>10,"b"=>1,"c"=>3,"d"=>30);$arr2=array_flip($arr);ksort($arr2);echo "<pre>";print_r($arr2);echo "</pre>";?>
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);?>
Multi-array sorting:
<? Php $ arr = array ("aaa", "bbbbbbbbbbb", "cc", "ddddd"); // requirement: // 1. sort by title length // 2. the title length becomes the key of the title string // extract the length of the value in the array and use it as a new array // strlen ($ val) 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 indicates the sort in ascending order. sort ($ lens); $ arr2 = array_combine ($ lens, $ arr ); // use the first array as the key corresponding to the second array, and return a new array echo "<pre>"; print_r ($ arr2); echo "</pre>";?>
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
<?php$str="php,js,html,ces,div";$arr=explode(",",$str);echo "<pre>";print_r($arr);echo "</pre>";?>
</Pre> <span style = "white-space: pre"> </span> 2. inplode (); combines the array into a string <span style = "white-space: pre"> </span> <pre name = "code" class = "php"> <? Php $ str = "php, js, html, ces, div"; $ arr = explode (",", $ str); $ str2 = implode ("-", $ arr); echo "<pre>"; print_r ($ str2); echo "</pre>" ;?>
<? Php $ str = "php, js, html, ces, div"; $ arr = explode (",", $ str); $ arr2 = array_reverse ($ arr ); // explain the values in the array in reverse order $ str2 = implode ("-", $ arr2); echo "<pre>"; print_r ($ str2 ); echo "</pre>" ;?>
Array_slice ();
<? Php // always truncate from back to back $ arr = array ("aa", "bb", "cc", "dd", "ee", "ff ", "gg"); $ arr2 = array_slice ($ arr,); // It indicates that two aa bb instances are intercepted from 0. $ arr3 = array_slice ($ arr ); // indicates the number from the back to the third, and two screenshots are taken. // ee ff echo "<pre>"; print_r ($ arr3 ); echo "</pre>" ;?>
Not only can be removed, but can also be added
<? Php $ arr = array ("aa", "bb", "cc", "dd", "ee", "ff", "gg "); $ arr2 = array_splice ($ arr, 0, 3, array ("hh", "ii", "jj", "kk ")); // directly retrieve the value of the original array and change the original array. The original array is to remove the remaining value echo "<pre>"; print_r ($ arr2 ); echo "</pre>"; echo "<pre>"; print_r ($ arr); echo "</pre>";?> Array_merge (); <? Php $ a = array ("aa", "bb", "cc"); $ B = array ("dd", "ee", "ff ", "gg"); $ arr = array_merge ($ a, $ B); echo "<pre>"; print_r ($ arr); echo "</pre>";?>
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.
<? Php $ arr = array ("aa", "bb", "cc", "dd", "ee", "ff", "gg "); // shuffle ($ arr) is randomly shuffled from the original array order; // get the first three $ arr2 = array_slice ($ arr, 0, 3) of the array ); echo "<pre>"; print_r ($ arr2); echo "</pre>";?>
// Outputs a four-character verification code at random:
<? Php // fetch an array of 1-9 a-z A-Z $ a = range (1, 9); $ B = range (a, z); $ c = range (A, z, z); // combine the three numbers and $ d = array_merge ($ a, $ B, $ c); // shuffle the merged array ($ d ); // obtain the first 4 digits after merging $ e = array_slice ($ d, 0, 4); // convert the $ e array into a string $ f = join ("", $ e); echo $ f;?>
Reprinted please indicate the source: http://blog.csdn.net/junzaivip
Php array usage
First of all, your writing method is really different. Now PHP is more generic.
(1) PHP, a weak language, does not need to be initialized like a rigorous language. It is automatically initialized when used. Therefore, the array can be used after it is declared. In addition, sometimes it does not need to be declared, such as writing $ I = 1.25 directly. If an error is reported in a rigorous language, PHP can do this.
(2) The default index of the array starts from 0 and then increases from 1. Count ($ arr) is the number of array elements. If n is the number of elements in the current array, $ arr [n-1] is the largest element in the index. Adding $ arr [n] is equivalent to adding an element behind the array. In fact, you can write $ arr [] = $ ss, with the same effect.
(3) An algorithm concept problem means that a natural number can be divisible by a prime number smaller than it, but it is not a prime number.
(4) No program error ....
Usage of foreach traversal array in PHP
The first format traverses the given $ a array. In each loop, the value of the current unit is assigned to $ B and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).
Since PHP 5, you can easily add & before $ B to modify the array unit. This method will assign values by reference instead of copying a value. For example:
<? Php
$ Arr = array (1, 2, 3, 4 );
Foreach ($ arr as & $ value ){
$ Value = $ value * 2;
}
// $ Arr is now array (2, 4, 6, 8)
?>
Var_dump ($ arr );
The second method is to re-assign values to each unit in the original $ arr file. If you do not understand the unit, you can download a php Manual Online.