php array array_filter function and array_slice function

WBOY
Release: 2016-07-28 08:26:35
Original
1064 people have browsed it
  1. /*
  2. array_filter() uses the callback function to filter the cells in the array
  3. array _filter(array,function)
  4. Parameter description: If the custom filter function returns true, the current value of the operated array will be included in the returned result array,
  5. and the result will be formed into a new array, if the original array is An associative array, the keys remain unchanged.
  6. */
  7. function delEmpty($val) {
  8. ​​​​if​($val===​ "" || $val === "php") { //When there are null values ​​and php values ​​in the array, change them back to false, that is, remove the ones in the array Empty values ​​and php values ​​
  9. return false;
  10. return
  11. true; }
  12. array
  13. (
  14. 'A'
  15. =>"Java", =>false,                                              =>true,
  16.                                                                                                                                                                 'e'e = & gt; null,
  17. 'g' = & gt; 0,
  18. 'g1' = & gt; '0' ,
  19.                                                               array_filter($input_array));
  20. print_r(array_filter($input_array
  21. ,
  22. "delEmpty")); The running result without callback function: can be seen , false, null, and true '' blanks and 0 are filtered out, and the subscript of the array has not changed.
  23. There is the result of the callback function:
    1. /**
    2. * array_slice() function takes out a segment from the array
    3. * array_slice(array array, int offset[, int length])
    4. * According to the offset and length parameters specified A sequence in the array array.
    5. * offset represents the starting position, length represents the length of this sequence.
    6. * true key does not change
    7. */  
    8. $input = array("Java""Php",   
    9.                "C++",  "C#",   
    10.                "Ruby""Object-c");  
    11.   
    12. $outputA = array_slice($input, 2);      // returns "C++", "C#", "Ruby", "Object-c"  
    13. $outputB = array_slice($input, -2, 1);  // returns "Ruby"  
    14. $outputC = array_slice($input, 1, 3);   // returns "Php", "C++", "C#"  
    15. print_r($outputA);  
    16. print_r($outputB);  
    17. print_r($outputC);  
    18. print_r(array_slice($input, 2, -1, true));  
    19. print_r(array_slice($input, 2, -1));  
    20. ?> 
    // 运行后 进行查看结果 php数组array_filter函数和array_slice函数

以上就介绍了 php数组array_filter函数和array_slice函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
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!