Home >Backend Development >PHP Tutorial >Summary of examples of using array processing functions in PHP

Summary of examples of using array processing functions in PHP

WBOY
WBOYOriginal
2016-07-25 08:43:17862browse
  1. #Change the case of array keys
  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 keys
  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 items in array1 but Values ​​not in any other parameter array
  15. #Different keys count
  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 includes everything in array1 but not in any
  22. #Other parameter arrays Different values ​​and keys are not counted
  23. print_r(array_diff($arr3,$arr4));
  24. echo "

    ";
  25. #array_fill ( int $start_index , int $num , mixed $value )
  26. #Fill an array with num entries using the value of the value parameter,
  27. #The key name starts with the start_index parameter.
  28. print_r(array_fill(-5,8,"banana"));
  29. echo "

    ";
  30. #array_flip ( array $trans )
  31. #Return a reversed array , for example, the key name in trans becomes the value,
  32. #And the value in trans becomes 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. #Return an array that contains all cells in arr1 The unit after the callback has been applied to
  38. #. The number of parameters accepted by callback should be consistent with the number of arrays passed to array_map()
  39. #function.
  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 cells of one or more arrays, and append the values ​​in one array After the previous array
  48. #. Returns the resulting array. If the input arrays have the same string key name,
  49. # then the values ​​will be merged into an array, which will go on recursively, so if a value itself
  50. # is an array, this function will follow the corresponding entry Merge it into another array. However, if
  51. #if the arrays have the same array key name, the latter value will not overwrite the original value, but will be appended to
  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. #Apply the callback function iteratively to each unit in the input array, thereby simplifying the array
  59. # into a single value. If the optional parameter initial is specified, the parameter will be treated as the
  60. #th element in the array. A value to process, or as the final return value if the array is empty. If the array is empty and no initial parameter is passed, array_reduce() returns NULL
  61. function rsum($v,$w){
  62. $v+. =$w;
  63. return $v;
  64. }
  65. function rmul($v,$w){
  66. $v*=$w;
  67. return $v;
  68. }
  69. $a=array(1,2,3,4 ,5);
  70. $x=array();
  71. $b=array_reduce($a,"rsum");
  72. $c=array_reduce($a,"rmul",10);
  73. $d=array_reduce($x ,"rsum",1);
  74. echo $b."tt".$c."tt".$d."n";
  75. echo "

    ";
  76. #array_replace ( array &$array , array &$array1 [, array &$array2 [, array &$... ]] )
  77. #Function replaces the value of the first array array with the value of the subsequent array element if a key exists. The first
  78. # array also exists in the second array, and its value will be replaced by the value in the second array. If a
  79. # key exists in the second array, but not in the first array, This
  80. # element will be created in the first array.If a key only exists in the first array, it will remain unchanged. If multiple replacement number
  81. #groups are passed, they will be processed in order, and subsequent arrays will overwrite the previous values.
  82. $base=array("orange","banana","apple","raspberry");
  83. $replacements=array(0=>"pineapple",4=>"cherry");
  84. $replacements2 =array(0=>"grape");
  85. #print_r(array_replace($base,$replacements,$replacements2));
  86. #echo "

    ";
  87. #array_splice ( array &$input , int $offset [, int $length [, array $ replacement ]] )
  88. #Remove the units specified by offset and length in the input array. If the replacement
  89. # parameter is provided, use the replacement array Unit replacement. Returns an array containing the removed cells
  90. # . Note that numeric key names in input are not preserved. If length is omitted, all parts of the array from
  91. # offset to the end are removed. If length is specified and is positive, this many cells are removed
  92. # . If length is specified and is a negative value, all elements from offset to length
  93. # inverse of the end of the array are removed. Tip: When replacement is given and you want to remove all cells from offset to
  94. # the end of the array, use count($input) as length.
  95. $input=array("red","green","blue","yellow");
  96. array_splice($input,1,-1);
  97. print_r($input);
  98. echo "

    ";
  99. #key ( array &$array )
  100. #Return the key name of the current unit in the array.
  101. $fruit=array("fruit1"=>"apple","fruit2"=>"orange","fruit3"=>"grape",
  102. "fruit4"=>"apple","fruit5" =>"apple");
  103. while($fruit_name=current($fruit)){
  104. if($fruit_name=='apple'){
  105. echo key($fruit)."
    ";
  106. }
  107. next($fruit);
  108. }
  109. echo "

    ";
  110. ?>
Copy code

PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn