PHP
HTML
CSS
JavaScript
jQuery
SQL
WordPress
PHP Reference

Quick Reference

The PHP array_filter() function filters the values of an array using a callback function, passing each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

				
					<?php
function test_odd($var) {
  return($var & 1);
}

$a1 = array(1, 2, 3, 4, 5);
print_r(array_filter($a1, 'test_odd'));
?>
				
			

Output

				
					Array ( [0] => 1 [2] => 3 [4] => 5 )
				
			

Syntax

				
					array_filter(array, callbackfunction, flag)
				
			

Parameters

ParameterDescription
arraySpecifies the array to filter (required)
callbackfunctionSpecifies the callback function to use
flagSpecifies what arguments are sent to callback

  • ARRAY_FILTER_USE_KEY - pass key as the only argument to callback (instead of the value)

  • ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback (instead of the value)

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.

PHP
HTML
CSS
JavaScript
jQuery
SQL
WordPress

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

Feedback

If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.