Experience PHP Array functions Summary _php tutorial

Source: Internet
Author: User
These days on the Internet to read a lot about PHP array functions related knowledge, feel Confucius "warm so know new" really not false, here is I summed up some experience, hope to everyone has help.

The array in PHP is actually an ordered graph, which is a type that maps values to keys. This type is optimized in many ways, so you can use it as a real array, or list (vector), hash list (an implementation of the graph), dictionaries, collections, stacks, queues, and more possibilities. Because you can use another PHP array as a value, you can also easily emulate a tree. Explaining these structures is beyond the scope of this manual, but you will find at least one example for each structure. To get more information on these structures, we recommend that you refer to the external works on this broad topic, and note some of my PHP array functions as follows:

1. Splitting a 1-d array into a 2-dimensional array array_chunk ()

 
  
  
  1. $ Input_array =array (' A ', ' B ', ' C ', ' d ', ' e ');

2. Compare 2 arrays, ARRAY_DIFF_ASSOC () or Array_diff (), if the return value is empty, representing two arrays of the same, otherwise it will be different.

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

 
 
  1. Functionodd ($var) {
  2. return ($var%2==1);
  3. }
  4. Functioneven ($var) {
  5. return ($var%2==0);
  6. }
  7. $ Array array1 =array ("a" =>1, "b" =>2, "c" =>3, "D "=>4," e "=>5);
  8. $ Array array2 =array (6,7,8,9,10,11,12);
  9. echo "Odd:n";
  10. Print_r (Array_filter ($array 1, "odd"));
  11. echo "Even:n";
  12. Print_r (Array_filter ($array 2, "even"));
  13. ?>

4.array_map () functions The callback function on the cell of the given array, its arguments can be an array, or it can be multiple arrays, and the parameters of the callback function must be the same as the parameters that call it.

 
 
  1. An example of a single argument that multiplies each value in the array by multiplying it by 3 times
  2. Functioncube ($n) {
  3. return$n* $n * $n;
  4. }
  5. $ a = Array (1,2,3,4,5);
  6. $ b = Array_map ("Cube", $a);
  7. Print_r ($b);
  8. ?>
  9. Examples of multiple array parameters
  10. Functionshow_spanish ($n, $m) {
  11. return "Thenumber$niscalled$minspanish";
  12. }
  13. Functionmap_spanish ($n, $m) {
  14. ReturnArray ($n=>$m);
  15. }
  16. $ a = Array (1,2,3,4,5);
  17. $ b = Array ("Uno", "dos", "Tres", "Cuatro", "Cinco");
  18. $ C = Array_map ("Show_spanish", $a, $b);
  19. Print_r ($c);
  20. $ D = Array_map ("Map_spanish", $a, $b);
  21. Print_r ($d);
  22. ?>
  23. Output results
  24. Printoutof$c
  25. Array
  26. (
  27. [0]= > Thenumber1iscalledunoinspanish
  28. [1]= > Thenumber2iscalleddosinspanish
  29. [2]= > Thenumber3iscalledtresinspanish
  30. [3]= > Thenumber4iscalledcuatroinspanish
  31. [4]= > Thenumber5iscalledcincoinspanish
  32. )

The above is about the PHP array function of the relevant summary, we have to memorize ah.


http://www.bkjia.com/PHPjc/446539.html www.bkjia.com true http://www.bkjia.com/PHPjc/446539.html techarticle these days on the Internet to read a lot about PHP array functions related knowledge, feel Confucius "warm so know new" really not false, here is I summed up some experience, hope to everyone has help ...

  • Related Article

    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.