Tribe__Utils__Array::map_or_discard( string|array $keys, array $map, bool $found = true )

Returns an array of values obtained by using the keys on the map; keys that do not have a match in map are discarded.

To discriminate from not found results and legitimately false values from the map the $found parameter will be set by reference.


Parameters

$keys

(string|array) (Required) One or more keys that should be used to get the new values

$map

(array) (Required) An associative array relating the keys to the new values.

$found

(bool) (Optional) When using a single key this argument will be set to indicate whether the mapping was successful or not.

Default value: true


Top ↑

Return

(array|mixed|false) An array of mapped values, a single mapped value when passing one key only or false if one key was passed but the key could not be mapped.


Top ↑

Source

File: src/Tribe/Utils/Array.php

	public static function map_or_discard( $keys, array $map, &$found = true ) {
		$hash   = md5( time() );
		$mapped = array();

		foreach ( (array) $keys as $key ) {
			$meta_key = Tribe__Utils__Array::get( $map, $key, $hash );
			if ( $hash === $meta_key ) {
				continue;
			}
			$mapped[] = $meta_key;
		}

		$found = (bool) count( $mapped );

		if ( is_array( $keys ) ) {
			return $mapped;
		}

		return $found ? $mapped[0] : false;
	}

Top ↑

Changelog

Changelog
Version Description
4.7.19 Introduced.