Implementation code of filtering empty array using array_filter of function in php

  • 2021-07-13 04:31:01
  • OfStack

Today, when looking through the manual to inquire about the use details of array_filter (), I saw a small key point: If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. If no callback function is given, all elements equal to FALSE will be removed. Isn't this just enough to filter an array of empty elements? Hurry and write an example to test the idea of 1:


$entry = array(               0 => ' Blue Hawaii Blog ',               1 => false,               2 => 1,               3 => null,               4 => '',               5 => 'https://www.ofstack.com',               6 => '0',              7 => array(),              8 => 0           ); $validarr = array_filter($entry); print_r($validarr); // Output: Array (     [0] => Blue Hawaii Blog     [2] => 1     [5] => https://www.ofstack.com )

Some array elements that can be converted to Boolean FALSE have been removed, which makes a very comprehensive filter for what we want to get a valid array.


Related articles: