PHP array Array_filter () function and array_slice () function

Source: Internet
Author: User

/*

Array_filter () filters cells in an array with a callback function

Array_filter (array,function)

Parameter description: If the custom filter function returns True, the current value of the array being manipulated is included in the returned result array.

And make a new array of results, if the original array is an associative array, the key name remains unchanged.

*/

function Delempty ($val) {

if ($val = = = "" | | $val = = = "php") {//when null and PHP values are present in the array, return false, that is, remove null values and PHP values from the array

return false;

}

return true;

}

$input _array = Array (' A ' = = ' Java ',

' B ' =>false,

' B1 ' =>true,

' C ' and ' = ',

' D ' and ' = ',

' E ' =>null,

' G ' =>0,

' G1 ' = ' 0 ',

' H ' = ' php ');

Print_r (Array_filter ($input _array));

Print_r (Array_filter ($input _array, "Delempty"));

?>


The result of running without a callback function:

As you can see, false,null, and the real ' blank ' and 0 are filtered, and the subscript of the array has not changed.

There is a result of the callback function running:

[PHP] view plaincopyprint?

/**

* the Array_slice () function takes a paragraph out of the array

* Array_slice (array array, int offset[, int length])

* A sequence of arrays in an array, as specified by the offset and length parameters.

* Offset indicates the start position, and length indicates how long the sequence is.

* 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));

?>


Operation Result:

  • 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.