Tribe__Utils__Array::usearch( mixed $needle, array $haystack, callable $callback )

Searches an array using a callback and returns the index of the first match.

This method fills the gap left by the non-existence of an array_usearch function.


Parameters

$needle

(mixed) (Required) The element to search in the array.

$haystack

(array) (Required) The array to search.

$callback

(callable) (Required) A callback function with signature fn($needle, $value, $key) :bool that will be used to find the first match of needle in haystack.


Top ↑

Return

(string|int|false) Either the index of the first match or false if no match was found.


Top ↑

Source

File: src/Tribe/Utils/Array.php

		public static function usearch( $needle, array $haystack, callable $callback ) {
			foreach ( $haystack as $key => $value ) {
				if ( $callback( $needle, $value, $key ) ) {
					return $key;
				}
			}

			return false;
		}

Top ↑

Changelog

Changelog
Version Description
5.0.0 Introduced.