Tribe__Utils__Array::parse_associative_array_alias( array $original, array $alias_map )
Build an array from migrating aliased key values to their canonical key values, removing all alias keys.
Contents
If the original array has values for both the alias and its canonical, keep the canonical’s value and discard the alias’ value.
Parameters
- $original
-
(array) (Required) An associative array of values, such as passed shortcode arguments.
- $alias_map
-
(array) (Required) An associative array of aliases: key as alias, value as mapped canonical. Example: [ 'alias' => 'canonical', 'from' => 'to', 'that' => 'becomes_this' ]
Return
(array)
Source
File: src/Tribe/Utils/Array.php
public static function parse_associative_array_alias( array $original, array $alias_map ) {
// Ensure array values.
$original = (array) $original;
$alias_map = static::filter_to_flat_scalar_associative_array( (array) $alias_map );
// Fail gracefully if alias array wasn't setup as [ 'from' => 'to' ].
if ( empty( $alias_map ) ) {
return $original;
}
$result = $original;
// Parse aliases.
foreach ( $alias_map as $from => $to ) {
// If this alias isn't in use, go onto the next.
if ( ! isset( $result[ $from ] ) ) {
continue;
}
// Only allow setting alias value if canonical value is not already present.
if ( ! isset( $result[ $to ] ) ) {
$result[ $to ] = $result[ $from ];
}
// Always remove the alias key.
unset( $result[ $from ] );
}
return $result;
}
Changelog
| Version | Description |
|---|---|
| 4.12.2 | Introduced. |