- /*
- array_filter() uses the callback function to filter the cells in the array
- array _filter(array,function)
- Parameter description: If the custom filter function returns true, the current value of the operated array will be included in the returned result array,
- and the result will be formed into a new array, if the original array is An associative array, the keys remain unchanged.
- */
- function delEmpty($val) {
- 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
- return false;
- return
- true; }
array- (
'A'- =>"Java", =>false, =>true,
- 'e'e = & gt; null,
- 'g' = & gt; 0,
- 'g1' = & gt; '0' ,
- array_filter($input_array));
- print_r(array_filter($input_array
, -
"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.
- There is the result of the callback function:
- /**
- * array_slice() function takes out a segment from the array
- * array_slice(array array, int offset[, int length])
- * According to the offset and length parameters specified A sequence in the array array.
- * offset represents the starting position, length represents the length of this sequence.
- * true key does not change
- */
- $input = array("Java", "Php",
- "C++", "C#",
- "Ruby", "Object-c");
-
- $outputA = array_slice($input, 2); // returns "C++", "C#", "Ruby", "Object-c"
- $outputB = array_slice($input, -2, 1); // returns "Ruby"
- $outputC = array_slice($input, 1, 3); // returns "Php", "C++", "C#"
- print_r($outputA);
- print_r($outputB);
- print_r($outputC);
- print_r(array_slice($input, 2, -1, true));
- print_r(array_slice($input, 2, -1));
- ?>
// 运行后 进行查看结果
以上就介绍了 php数组array_filter函数和array_slice函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。