Summary of PHP array Functions

Source: Internet
Author: User

I have read a lot of related knowledge about PHP array functions on the Internet over the past few days, and I think that Kong's "understanding the new" is true and false. Here is my summary of some experience and I hope to help you.

Arrays in PHP are actually an ordered graph, which is a type that maps values to keys. This type has been optimized in many ways, so you can use it as a real array, or a list (vector), a hash (an implementation of a graph), a dictionary, set, stack, queue, and more possibilities. Because another PHP array can be used as the value, and the tree can be easily simulated. Interpreting these structures is beyond the scope of this manual, but you will find at least one example for each structure. For more information about these structures, we recommend that you refer to external works on this broad topic. Here are some of my PHP array functions:

1. Cut a 1-dimension component into a 2-dimensional array array_chunk ()

 
 
  1. $input_array=array('a','b','c','d','e');  
  2. print_r(array_chunk($input_array,2)); 

2. Compare two Arrays: array_diff_assoc () or array_diff (). If the returned value is null, the two arrays are the same. Otherwise, the two arrays are different.

3. Use a function to filter the value array_filter () in the array ()

 
 
  1. functionodd($var){  
  2. return($var%2==1);  
  3. }  
  4. functioneven($var){  
  5. return($var%2==0);  
  6. }  
  7. $arrayarray1=array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);  
  8. $arrayarray2=array(6,7,8,9,10,11,12);  
  9. echo"Odd:n";  
  10. print_r(array_filter($array1,"odd"));  
  11. echo"Even:n";  
  12. print_r(array_filter($array2,"even"));  
  13. ?> 

4. array_map () applies the callback function to the units of the given array. Its parameters can be an array or multiple arrays, the parameters of the callback function must be the same as those of the callback function.

 
 
  1. // Example of a single parameter, multiply each value in the array by its power 3
  2. Functioncube ($ n ){
  3. Return $ n * $ n;
  4. }
  5.  
  6. $A=Array(1, 2, 3, 4, 5 );
  7. $B=Array_map("Cube", $ );
  8. Print_r ($ B );
  9. ?> 
  10.  
  11. // Example of multiple array parameters
  12. Functionshow_Spanish ($ n, $ m ){
  13. Return "Thenumber $ niscalled $ minSpanish ";
  14. }
  15.  
  16. Functionmap_Spanish ($ n, $ m ){
  17. Returnarray ($N=>$ M );
  18. }
  19.  
  20. $A=Array(1, 2, 3, 4, 5 );
  21. $B=Array("Uno", "dos", "tres", "cuatro", "cinco ");
  22.  
  23. $C=Array_map("Show_Spanish", $ a, $ B );
  24. Print_r ($ c );
  25. $D=Array_map("Map_Spanish", $ a, $ B );
  26. Print_r ($ d );
  27. ?> 
  28. // Output result
  29. // Printoutof $ c
  30. Array
  31. (
  32. [0] =>Thenumber1iscalledunoinSpanish
  33. [1] =>Thenumber2iscalleddosinSpanish
  34. [2] =>Thenumber3iscalledtresinSpanish
  35. [3] =>Thenumber4iscalledcuatroinSpanish
  36. [4] =>Thenumber5iscalledcincoinSpanish
  37. )

The above is a summary of PHP array functions.


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.