Summary of use examples of array processing functions in PHP

Source: Internet
Author: User
Summary of use examples of array processing functions in PHP

  1. # Changing the case sensitivity of the array key
  2. $ Arr1 = array ("a" => "Lamp", "db" => "database", "LANGUAGE" => "PHP ");
  3. Print_r (array_change_key_case ($ arr1, CASE_UPPER ));
  4. Echo"
    ";
  5. Print_r (array_change_key_case ($ arr1, CASE_LOWER ));
  6. Echo"
    ";
  7. # Split an array into multiple third parameters to set whether to retain the key
  8. $ Arr2 = array ('A', 'B', 'C', 'D', 'e', 'F', 'g ');
  9. Print_r (array_chunk ($ arr2, 2, true ));
  10. Echo"
    ";
  11. Print_r (array_chunk ($ arr2, 2, false ));
  12. Echo"
    ";
  13. # Array array_diff_assoc (array $ array1, array $ array2 [, array $...]) returns an array,
  14. # This array includes all the values in the array of array1 but not in any other parameter array.
  15. # Different keys
  16. $ Arr3 = array ('a' => 'green', 'B' => 'brown ', 'C' => 'red ');
  17. $ Arr4 = array ('a' => 'green', 'yellow', 'red ');
  18. Print_r (array_diff_assoc ($ arr3, $ arr4 ));
  19. Echo"
    ";
  20. # Array_diff (array $ array1, array $ array2 [, array $...])
  21. # Returns an array that contains all but not any
  22. # The values in other parameter arrays are not counted if the keys are different.
  23. Print_r (array_diff ($ arr3, $ arr4 ));
  24. Echo"
    ";
  25. # Array_fill (int $ start_index, int $ num, mixed $ value)
  26. # Use the value of the value parameter to fill an array with num entries,
  27. # The key name is specified by the start_index parameter.
  28. Print_r (array_fill (-5, 8, "banana "));
  29. Echo"
    ";
  30. # Array_flip (array $ trans)
  31. # Returns an inverted array. for example, the key name in trans is changed to a value,
  32. # The value in trans is the key name.
  33. $ Arr5 = array ('a' => '1', "B" => "2", "c", "d", "e ");
  34. Print_r (array_flip ($ arr5 ));
  35. Echo"
    ";
  36. # Array_map (callback $ callback, array $ arr1 [, array $...])
  37. # Returns an array containing all the elements in arr1 that have passed the callback function
  38. # Subsequent units. The number of parameters accepted by callback should be passed to array_map ()
  39. # The number of functions is the same.
  40. Function cube ($ n ){
  41. Return $ n * $ n;
  42. }
  43. $ Arr6 = array (1, 2, 3, 4, 5 );
  44. Print_r (array_map ("cube", $ arr6 ));
  45. Echo"
    ";
  46. # Array_merge_recursive (array $ array1 [, array $...])
  47. # Merge the units of one or more arrays. the values in an array are appended to the previous array.
  48. . Returns an array of results. If the input array has the same string key name,
  49. # Then these values will be merged into an array, which will be recursive, so if a value itself
  50. # Is an array. this function combines it into another array according to the corresponding entries. However, for example
  51. # If the array has the same array key name, the next value will not overwrite the original value, but will be appended
  52. .
  53. $ Arr7 = array ("color" => array ("favorite" => "red"), 5 );
  54. $ Arr8 = array (10, array ("favorite" => "yellow", "blue "));
  55. Print_r (array_merge_recursive ($ arr7, $ arr8 ));
  56. Echo"
    ";
  57. # Array_reduce (array $ input, callback $ function [, int $ initial]
  58. # Iterate the callback function to every unit in the input array, so that the array
  59. # Simplified to a single value. If the optional parameter initial is specified, the parameter is treated as
  60. # Process a value, or use the final return value if the array is empty. If the array is empty and no
  61. # If the initial parameter is passed, array_reduce () returns NULL.
  62. Function rsum ($ v, $ w ){
  63. $ V + = $ w;
  64. Return $ v;
  65. }
  66. Function rmul ($ v, $ w ){
  67. $ V * = $ w;
  68. Return $ v;
  69. }
  70. $ A = array (1, 2, 3, 4, 5 );
  71. $ X = array ();
  72. $ B = array_reduce ($ a, "rsum ");
  73. $ C = array_reduce ($ a, "rmul", 10 );
  74. $ D = array_reduce ($ x, "rsum", 1 );
  75. Echo $ B. "\ t". $ c. "\ t". $ d. "\ n ";
  76. Echo"
    ";
  77. # Array_replace (array & $ array, array & $ array1 [, array & $ array2 [, array & $...])
  78. # The function replaces the value of the first array with the value of the following array element. If a key exists in the first
  79. # An Array also exists in the second array, and its value will be replaced by the value in the second array. If
  80. # If the key exists in the second array but does not exist in the first array, this
  81. # Element. If a key only exists in the first array, it remains unchanged. If multiple replicas are passed
  82. # Groups, which are processed sequentially, and the subsequent arrays overwrite the previous values.
  83. $ Base = array ("orange", "banana", "apple", "raspberry ");
  84. $ Replacements = array (0 => "pineapple", 4 => "cherry ");
  85. $ Replacements2 = array (0 => "grape ");
  86. # Print_r (array_replace ($ base, $ replacements, $ replacements2 ));
  87. # Echo"
    ";
  88. # Array_splice (array & $ input, int $ offset [, int $ length [, array $ replacement])
  89. # Remove the units specified by offset and length from the input array. if replacement is provided
  90. # Parameter, which is replaced by cells in the replacement array. Returns an array containing the removed cells.
  91. #. Note that the number key name in input is not retained. If length is omitted, remove
  92. # Offset all parts to the end. If length is specified and it is a positive value, so many units are removed.
  93. #. If length is specified and it is a negative value, remove the reciprocal length from offset to the end of the array.
  94. # All units in the middle. Tips: remove from offset
  95. # Count ($ input) is used as the length for all cells at the end of the array.
  96. $ Input = array ("red", "green", "blue", "yellow ");
  97. Array_splice ($ input, 1,-1 );
  98. Print_r ($ input );
  99. Echo"
    ";
  100. # Key (array & $ array)
  101. # Return the key name of the current cell in the array.
  102. $ Fruit = array ("fruit1" => "apple", "fruit2" => "orange", "fruit3" => "grape ",
  103. "Fruit4" => "apple", "fruit5" => "apple ");
  104. While ($ fruit_name = current ($ fruit )){
  105. If ($ fruit_name = 'apple '){
  106. Echo key ($ fruit )."
    ";
  107. }
  108. Next ($ fruit );
  109. }
  110. Echo"
    ";
  111. ?>


PHP

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.