Understanding of several array functions in PHP

WBOY
Release: 2016-07-25 09:07:24
Original
877 people have browsed it
  1. // Find sum
  2. function rsum($v, $w)
  3. {
  4. return $v;
  5. }
  6. // Find deposition
  7. function rmul($v, $w)
  8. {
  9. return $v;
  10. }
  11. $a = array(1, 2, 3, 4, 5);
  12. $x = array();
  13. $b = array_reduce($a, "rsum");
  14. echo 'result:';
  15. print_r($b);
  16. echo '
    ';
  17. // The above output is 15, which is equivalent to array_sum
  18. $c = array_reduce($a, "rsum", 1) ;
  19. echo 'result:';
  20. print_r($c);
  21. echo '
    ';
  22. // Output 16, which is equivalent to array_sum plus 1
  23. $d = array_reduce($a, "rmul", 10);
  24. echo 'result:';
  25. print_r($d);
  26. echo '
    ';
  27. // 1200, equivalent to 10 * 1 * 2 * 3 * 4 * 5
  28. $d = array_reduce($a, "rmul");
  29. echo 'result:';
  30. print_r($d);
  31. echo '
    ';
  32. // 0, equivalent to 0 * 1 * 2 * 3 * 4 * 5
  33. $d = array_reduce($x, "rsum", 1);
  34. echo 'result:';
  35. print_r($d);
  36. // Output 1 Since $x is empty, So directly return the initial
Copy code

2. array_chunk function

(PHP 4 >= 4.2.0, PHP 5)

array_chunk — split an array into multiple illustrate array array_chunk ( array$input , int$size [, bool$preserve_keys ] )

array_chunk() splits an array into multiple arrays, where the number of cells in each array is determined by size (that is, each array has size elements). The last array may have a few fewer elements. The resulting array is a cell in a multidimensional array, with indexes starting from zero.

Set the optional parameter preserve_keys to TRUE to enable PHP to retain the original key names in the input array. If you specify FALSE, each result array will be indexed with a new number starting from zero. The default value is FALSE. This function is relatively easy to understand, so I won’t say much about it.

3. array_flip function

(PHP 4, PHP 5)

array_flip — swap keys and values ​​in an array illustrate array array_flip (array$trans)

array_flip() returns a reversed array, for example, the key names in trans become values, and the values ​​in trans become key names.

Note that the value in trans needs to be a legal key name, for example, it needs to be integer or string. A warning will be emitted if the value is of the wrong type, and the key/value pair in question will not be reversed.

If the same value appears multiple times, the last key name will be used as its value, and all others are lost.

array_flip() returns FALSE if it fails.

Note: Call it twice in a row to remove duplicates!

4. array_filter function

(PHP 4 >= 4.0.6, PHP 5)

array_filter — Filter elements in an array using a callback function illustrate array array_filter ( array$input [, callback$callback ] )

array_filter() passes each value in the input array to the callback function in turn. If the callback function returns TRUE, the current value of the input array will be included in the returned result array. The key names of the array remain unchanged.

array_filter() example

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

Output: Odd : Array ( [a] => 1 [c] => 3 [e] => 5 ) Even: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 )

Users should not modify the array itself in the callback function. For example, add/delete cells or unset the array being acted upon by array_filter(). The behavior of this function is undefined if the array changes.

If no callback function is provided, array_filter() will delete all entries in input whose value is FALSE. See Converting to Boolean for more information.

  1. $entry = array(
  2. 0 => 'foo',
  3. 1 => false,
  4. 2 => -1,
  5. 3 => null,
  6. 4 => ''
  7. );
  8. print_r(array_filter($entry));
  9. ?>
Copy code

The above example output: Array ( [0] => foo [2] => -1 )



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!