PHP Seventh Lesson Array Usage 2

Source: Internet
Author: User
Tags explode shuffle

Learning outline:

1. Understanding Array Functions

2. Random Output Verification Code

1. Array functions:



Array functions:
Role: Provides a number of very useful code snippets that are written by the official, increasing the speed of writing.

1. Key-value manipulation functions for arrays
2. Elements and uniqueness of the statistical array
3. Functions for handling arrays using callback functions
4. Sorting Functions for arrays
5. Splitting, merging, decomposition and binding functions
6. Arrays and data structures
7. Other useful array-handling functions


An array of key-value manipulation functions:
1.array_values ();

Simulation gets the value of key and value
<?php$arr=array ("Name" = "user1", "age" = "+", "sex" = "man"), foreach ($arr as $key = = $val) {$keys []=$ key; $vals []= $val;} echo "<pre>";p Rint_r ($keys), echo "</pre>", echo "




Use of 2.array_values
<?php$arr=array ("Name" = "user1", "age" = "a", "sex" = "man"), $keys =array_values ($arr); echo "<pre > ";p rint_r ($keys); echo" </pre> ";? >



Array_values ();//Gets the value in the array
Array_keys ();//Gets the health of the array
In_array ();//Check if a value is in the array
Array_key_exists ();//Check if a key is in the array
Array_flip ();//Key and value swap
Array_reverse (); Inversion of values in an array


Elements and uniqueness of a statistical array
1.count ();
2.array_count_values ();//Statistics The number of occurrences of each value in the array.
3.array_unique ();//delete duplicates in the array


Functions for handling arrays using callback functions:
1.array_filter ();
<?php$arr=array ("user1" =>70,60,80,78,34,34,34,56,78,78), function older ($var) {return ($var >60);} $arr 2=array_filter ($arr, "older"), echo "<pre>";p Rint_r ($arr 2); echo "</pre>";? >


2.array_map ();


Reference parameters:
Requirements: Array values are added from 1


Function Show (& $arr) {
foreach ($arr as $key = = $val) {
$arr [$key]= $val +1;


}


}




Sorting Functions for arrays
1.sort (); Ascending, do not retain key
2.rsort (); Descending, not preserving key
3.asort (); Ascending, reserved key
4.arsort (); Descending, reserved key
5.ksort (); Sort Ascending by key
6.krsort (); Sort Descending by key
7.natsort () Natural number sort ascending, than slice img2.jpg
8.natcasesort (); Ignore case ascending order
9.multisort (); Sort by most groups




Ksort ();
<?php$arr=array ("user1" =>10, "B" =>1, "C" =>3, "D" =>30); $arr 2=array_flip ($arr); Ksort ($arr 2); echo " <pre> ";p Rint_r ($arr 2); echo" </pre> ";? >




Natsort ();






Most groups are sorted by:
<?php$arr=array ("AAA", "BBBBBBBBB", "CC", "ddddd");//demand://1. Sort by title length//2. The length of the header is changed to the title string key//the lengths of the value in the array are taken out, and takes the length of the string as a new array//strlen ($val) foreach ($arr as  $val) {    $lens []=strlen ($val);} Array_multisort ($lens, SORT_ASC, $arr);//Sort by array, sort the second array according to the first, sort_asc to sort sort  ($lens) in ascending order; $arr 2=array_ Combine ($lens, $arr);//The first array returns a new array as the key corresponding to the second array, echo "<pre>";p Rint_r ($arr 2); echo "</pre>";? >






Splitting, merging, decomposing and binding functions
1.explode ();
2.inplode ();//join ();
3.array_slice (); Intercept of arrays
4.array_splice (); Clipping of arrays
5.array-merge (); Merge most groups
6.array_combine (); Merge array, two arrays, previous array as key, last array as value
7.array_intersect (); Find the intersection of two arrays
8.array_diff (); Find out the difference between two arrays, according to the first parameter
9.array_pop (); POPs a value from the last, returns the popup value
10.array_push (); Press a value from the last position to return the number of elements
11.array_shift (); Remove a value from the previous position of the wash
12.array_unshift (); Press a value from the top position


<?php$str= "Php,js,html,ces,div", $arr =explode (",", $str), echo "<pre>";p Rint_r ($arr), echo "</pre>";? >

</pre><span style= "White-space:pre" ></span>2.inplode (); Combine arrays into strings <span style= "White-space: Pre "></span><pre name=" code "class=" PHP "><?php$str=" Php,js,html,ces,div "; $arr =explode (", ", $str $str 2=implode ("-", $arr), echo "<pre>";p Rint_r ($str 2); echo "</pre>";? >






<?php$str= "Php,js,html,ces,div", $arr =explode (",", $str), $arr 2=array_reverse ($arr);//The values in the array are reversed $str2=implode ( "-", $arr 2), echo "<pre>";p Rint_r ($str 2); echo "</pre>";? >





Array_slice ();
<?php//interception is always intercepted from the back    $arr = Array ("AA", "BB", "CC", "dd", "EE", "FF", "GG");    $arr 2 = Array_slice ($arr, 0,2);//0 intercept 2  AA bb    $arr 3 = Array_slice ($arr, -3,2);//indicates the position from the back to the 3, starting to intercept 2//ee  ff    echo "<pre>";     Print_r ($arr 3);     echo "</pre>";?>

Not only is it removed, but it can be added


<?php    $arr = Array ("AA", "BB", "CC", "dd", "EE", "FF", "GG");    $arr 2 = array_splice ($arr, 0, 3, array ("HH", "II", "JJ", "KK")), or the value of the original array is directly taken, and the original array is changed, the original array is the value left after taking away    echo "<pre> ";    Print_r ($arr 2);    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-handling functions:
1.array_rand ();//randomly take a key
2.range ();//Take out a range of arrays
3.shuffle ();//Disturb the function of the array
4.array_sum ();//Calculate all the people in the array and (calculate the score)
If the sum of the keys of an array is computed, the Array_flip () can be swapped with the health and value of the arrays, and then the sum of kin can be calculated.








<?php    $arr = Array ("AA", "BB", "CC", "dd", "EE", "FF", "GG");    The original array order is randomly disturbed    shuffle ($arr);    Remove the first 3 $arr of the array    2= array_slice ($arr, 0, 3);    echo "<pre>";    Print_r ($arr 2);    echo "</pre>";? >






Random output four-bit character verification code implementation:
<?php//Remove 1-9 A-z array    $a = range (1, 9);    $b = Range (A, z);    $c = Range (A, Z);    Combine 3 numbers and    $d = Array_merge ($a, $b, $c);    The merged array is scrambled to    shuffle ($d);    Take the first 4 digits of the merged    $e = array_slice ($d, 0, 4);    Change the $e array to a string    $f = Join ("", $e);    Echo $f;? >


Reprint Please specify source: HTTP://BLOG.CSDN.NET/JUNZAIVIP

PHP Seventh Lesson Array Usage 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.