An example summary of array processing functions in PHP

Source: Internet
Author: User
  1. #改变数组键的大小写
  2. $arr 1=array ("a" = "Lamp", "db" = "database", "LANGUAGE" and "PHP");
  3. Print_r (Array_change_key_case ($arr 1,case_upper));
  4. echo "
    ";
  5. Print_r (Array_change_key_case ($arr 1,case_lower));
  6. echo "
    ";
  7. #将一个数组分割成多个 the third parameter sets whether the key is reserved
  8. $arr 2=array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ');
  9. Print_r (Array_chunk ($arr 2,2,true));
  10. echo "
    ";
  11. Print_r (Array_chunk ($arr 2,2,false));
  12. echo "
    ";
  13. #array Array_diff_assoc (array $array 1, array $array 2 [, array $ ...]) returns an array,
  14. #该数组包括了所有在 Array1 but not the values in any other parameter array
  15. #键不同也算
  16. $arr 3=array (' a ' + = ' green ', ' b ' = ' brown ', ' c ' = ' red ');
  17. $arr 4=array (' a ' = ' green ', ' yellow ', ' red ');
  18. Print_r (Array_diff_assoc ($arr 3, $arr 4));
  19. echo "
    ";
  20. #array_diff (array $array 1, array $array 2 [, array $ ...])
  21. #返回一个数组, the array includes all of the array1 but not any
  22. #其它参数数组中的值, different keys are not counted
  23. Print_r (Array_diff ($arr 3, $arr 4));
  24. echo "
    ";
  25. #array_fill (int $start _index, int $num, mixed $value)
  26. The value of the #用 value parameter populates an array with num entries,
  27. #键名由 the start specified by the Start_index parameter.
  28. Print_r (Array_fill ( -5,8, "banana"));
  29. echo "
    ";
  30. #array_flip (Array $trans)
  31. #返回一个反转后的 array, such as the key name in trans, becomes a value,
  32. The value in the #而 trans is the key name.
  33. $arr 5=array (' a ' = ' 1 ', "b" = "2", "C", "D", "E");
  34. Print_r (Array_flip ($arr 5));
  35. echo "
    ";
  36. #array_map (callback $callback, array $arr 1 [, Array $ ...]
  37. #返回一个数组, the array contains all the cells in the arr1 that have been callback
  38. #之后的单元. The number of parameters accepted by callback should be passed to Array_map ()
  39. #函数的数组数目一致.
  40. function cube ($n) {
  41. return $n * $n;
  42. }
  43. $arr 6=array (1,2,3,4,5);
  44. Print_r (Array_map ("Cube", $arr 6));
  45. echo "
    ";
  46. #array_merge_recursive (array $array 1 [, Array $ ...])
  47. #将一个或多个数组的单元合并起来, the value in an array is appended to the previous array
  48. #的后面. Returns an array as the result. If the input array has the same string key name,
  49. #则这些值会被合并到一个数组中去, this will recursively go down, so if a value itself
  50. #是一个数组, this function merges it into another array according to the corresponding entry. However, as
  51. #果数组具有相同的数组键名, the latter value will not overwrite the original value, but is appended to the
  52. #后面.
  53. $arr 7=array ("Color" =>array ("favorite" = "Red"), 5);
  54. $arr 8=array (10,array ("favorite" = "yellow", "blue"));
  55. Print_r (Array_merge_recursive ($arr 7, $arr 8));
  56. echo "
    ";
  57. #array_reduce (array $input, callback $function [, int $initial]
  58. The #将回调函数 function iterates through each cell in the input array, thus the array
  59. #简化为单一的值. If an optional parameter, initial, is specified, the parameter will be treated as an array of
  60. #一个值来处理, or, if the array is empty, as the final return value. If the array is empty and no
  61. #有传递 the initial parameter, 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\t ". $c." \t\t ". $d." \ n ";
  76. echo "
    ";
  77. #array_replace (array & $array, array & $array 1 [, Array & $array 2 [, Array &$ ...])
  78. #函数使用后面数组元素的值替换第一个 the values of array arrays. If a key exists in the first
  79. #个数组同时也存在于第二个数组, its value will be replaced by the value in the second array. If a
  80. #键存在于第二个数组, but does not exist in the first array, this is created in the first array.
  81. #元素. If a key exists only in the first array, it will remain unchanged. If more than one number of replacements are passed
  82. #组, they will be processed sequentially, followed by an array that overrides the previous value.
  83. $base =array ("Orange", "banana", "apple", "raspberry");
  84. $replacements =array (0=> "Pineapple",4=> "cherry");
  85. $replacements 2=array (0=> "Grape");
  86. #print_r (Array_replace ($base, $replacements, $replacements 2));
  87. #echo "
    ";
  88. #array_splice (array & $input, int $offset [, int $length [, array $ replacement]])
  89. #把 input array is removed by the unit specified by offset and length, if replacement is provided
  90. # parameter is replaced with a cell in the replacement array. Returns an array containing the cells that have been removed
  91. # 。 Note the numeric key names in input are not retained. If length is omitted, the group is removed from the
  92. # Offset to the end of all parts. If length is specified and positive, so many cells are removed
  93. # 。 If length is specified and is negative, remove the inverse length from offset to the end of the array
  94. # So far in the middle of all the units. Tip: When replacement is given, remove the offset to
  95. # when all the cells at the end of the array, count ($input) as length.
  96. $input =array ("Red", "green", "blue", "yellow");
  97. Array_splice ($input, 1,-1);
  98. Print_r ($input);
  99. echo "
    ";
  100. #key (Array & $array)
  101. #返回数组中当前单元的键名.
  102. $fruit =array ("fruit1" = "Apple", "fruit2" and "Orange", "fruit3" and "Grape",
  103. "FRUIT4" = "Apple", "fruit5" and "Apple";
  104. while ($fruit _name=current ($fruit)) {
  105. if ($fruit _name== ' Apple ') {
  106. echo Key ($fruit). "
    ";
  107. }
  108. Next ($fruit);
  109. }
  110. echo "
    ";
  111. ?>
Copy Code
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.