New array functions in PHP 8.4: Making them available in earlier versions

Created on: | Last updated on:

PHP 8.4 is coming soon, and it will introduce a few new and useful functions. In WP core, there have historically been so-called pluggable functions, allowing WP plugins and themes to use new functions as long as WP is running on a certain version. Here is my take on the new functions in PHP 8.4:

if(!function_exists('array_find')){
	function array_find(array $array, callable $callback){
		foreach($array as $value){
			if($callback($value)){
				return $value;
			}
		}
		return null;
	}
}

if(!function_exists('array_find_key')){
	function array_find_key(array $array, callable $callback){
		foreach($array as $key => $value){
			if($callback($value)){
				return $key;
			}
		}
		return null;
	}
}

if(!function_exists('array_any')){
	function array_any(array $array, callable $callback){
		foreach($array as $value){
			if($callback($value)){
				return true;
			}
		}
		return false;
	}
}

if(!function_exists('array_all')){
	function array_all(array $array, callable $callback){
		foreach($array as $value){
			if(!$callback($value)){
				return false;
			}
		}
		return true;
	}
}

To test it, here is some code you can run to see how it behaves.

$array = [1, 3, -7, 9];

// 1. array_find: Find the first negative number in the array
$found = array_find($array, function($value){
    return $value < 0;
});
echo "First negative number: " . ($found !== null ? $found : "None found") . "\n"; 
// Output: First negative number: -7

// 2. array_find_key: Find the key of the first negative number
$found_key = array_find_key($array, function($value){
    return $value < 0;
});
echo "Key of first negative number: " . ($found_key !== null ? $found_key : "None found") . "\n";
// Output: Key of first negative number: 2

// 3. array_any: Check if any number in the array is negative
$any_negative = array_any($array, function($value){
    return $value < 0;
});
echo "Any negative number: " . ($any_negative ? "Yes" : "No") . "\n";
// Output: Any negative number: Yes

// 4. array_all: Check if all numbers in the array are positive
$all_positive = array_all($array, function($value){
    return $value > 0;
});
echo "All positive numbers: " . ($all_positive ? "Yes" : "No") . "\n";
// Output: All positive numbers: No

If this makes it into WP core soon, it will be great for implementing these functions into the plugins and themes codebase.

Leave a Reply

Your email address will not be published. Required fields are marked *

WP Speed Doctor Copyright © 2026
The WordPress® trademark is the intellectual property of the WordPress Foundation, and the Woo® and WooCommerce® trademarks are the intellectual property of WooCommerce, Inc. Uses of the WordPress®, Woo®, and WooCommerce® names in this website are for identification purposes only and do not imply an endorsement by WordPress Foundation or WooCommerce, Inc. wpspeeddoctor.com is not endorsed or owned by, or affiliated with, the WordPress Foundation or WooCommerce, Inc.
Top