PHP Seventh Lesson Array Usage 2_php Tutorial

Source: Internet
Author: User

PHP Seventh Lesson Array Usage 2


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
 
  "User1", "age" = "a", "sex" = "man"), foreach ($arr as $key + $val) {$keys []= $key; $vals []= $val;} echo "
";p Rint_r ($keys); echo"
"; echo" "; echo"
";p Rint_r ($vals); echo"
";? >




Use of 2.array_values
 
  "User1", "age" = "a", "sex" = "man"), $keys =array_values ($arr); echo "
";p Rint_r ($keys); echo"
";? >



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 ();
 
  70,60,80,78,34,34,34,56,78,78); function older ($var) {return ($var >60);} $arr 2=array_filter ($arr, "older"); echo "
";p Rint_r ($arr 2); echo"
";? >


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 ();
 
  "B" =>1, "C" =>3, "D" =>30), $arr 2=array_flip ($arr); Ksort ($arr 2); echo "
";p Rint_r ($arr 2); echo"
";? >




Natsort ();
 
  






Most groups are sorted by:
 
  ";p Rint_r ($arr 2); echo"
";? >





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


 
  ";p Rint_r ($arr); echo"
";? >
2.inplode (); Combine arrays into strings
 
  ";p Rint_r ($str 2); echo"
";? >





 
  ";p Rint_r ($str 2); echo"
";? >




Array_slice ();
 
  ";     Print_r ($arr 3);     echo "
";?>
Not only is it removed, but it can be added


 
  ";    Print_r ($arr 2);    echo "
"; echo "
";    Print_r ($arr);    echo "
";? >array_merge (); "; Print_r ($arr); echo "";? >





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.








 
  ";    Print_r ($arr 2);    echo "
";? >





Random output four-bit character verification code implementation:
 
  


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

http://www.bkjia.com/PHPjc/871186.html www.bkjia.com true http://www.bkjia.com/PHPjc/871186.html techarticle PHP Seventh Lesson Array Usage 2 Learning outline: 1. Understanding array functions 2. Random output verification code 1. Array functions: Array functions://function: Provides a lot of official writing of very useful code snippets, mentioning ...

  • 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.