w3resource

PHP: array_filter() function

PHP: Filters elements of an array using a callback function

The array_filter() function passes each value of a given array to a user defined function. If the user defined function allows, the current value from the array is returned into the result array.

Note: The function Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from the array is returned into the result array. Array keys are preserved.

Version:

(PHP 4 and above)

Syntax:

array_filter(input_array, user_defined_function)

Parameter:

Name Description Required /
Optional
Type
input_array The input array. Required Array
user_defined_function The user defined function. If no user-defined function is supplied, all entries of input array equal to FALSE. Required
-

Return value:

The filtered array.

Value Type: Array

Example:

<?php
function my_function($item_values)
{
if ($item_values>100)
{
return true;
}
return false;
}
$item_list=array("Item1" => 100, "Item2" => 200, "Item3" => 125, "Item4" => 100);
print_r(array_filter($item_list,"my_function"));
?>

Output:

Array ( [Item2] => 200 [Item3] => 125 )

Pictorial Presentation:

php function reference: array_filter() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous:array_fill
Next: array_flip



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

PHP: Correct file permissions for WordPress

When you setup WP you (the webserver) may need write access to the files. So the access rights may need to be loose.

chown www-data:www-data  -R * # Let Apache be owner
find . -type d -exec chmod 755 {} \;  # Change directory permissions rwxr-xr-x
find . -type f -exec chmod 644 {} \;  # Change file permissions rw-r--r-

After the setup you should tighten the access rights, according to Hardening WordPress all files except for wp-content should be writable by your user account only. wp-content must be writable by www-data too.

chown <username>:<username>  -R * # Let your useraccount be owner
chown www-data:www-data wp-content # Let apache be owner of wp-content

Maybe you want to change the contents in wp-content later on. In this case you could

  • temporarily change to the user to www-data with su,
  • give wp-content group write access 775 and join the group www-data or
  • give your user the access rights to the folder using ACLs.

Whatever you do, make sure the files have rw permissions for www-data.

Ref : https://bit.ly/3hcrTkL

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook