diff --git a/README.md b/README.md index d48c201..6739e77 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ A library for array manipulations. * [set](/docs/classes/StellarWP/Arrays/Arr.md#set) * [shape_filter](/docs/classes/StellarWP/Arrays/Arr.md#shape_filter) * [shuffle](/docs/classes/StellarWP/Arrays/Arr.md#shuffle) + * [sort_by_priority](/docs/classes/StellarWP/Arrays/Arr.md#sort_by_priority) * [sort_recursive](/docs/classes/StellarWP/Arrays/Arr.md#sort_recursive) * [sort_recursive_desc](/docs/classes/StellarWP/Arrays/Arr.md#sort_recursive_desc) * [stringify_keys](/docs/classes/StellarWP/Arrays/Arr.md#stringify_keys) diff --git a/docs/classes/StellarWP/Arrays/Arr.md b/docs/classes/StellarWP/Arrays/Arr.md index 71bd99f..8a3cf0a 100644 --- a/docs/classes/StellarWP/Arrays/Arr.md +++ b/docs/classes/StellarWP/Arrays/Arr.md @@ -344,7 +344,7 @@ public static except(array $array, array|string|int|float $keys): array Determine if the given key exists in the provided array. ```php -public static exists(\ArrayAccess|array $array, string|int $key): bool +public static exists(\ArrayAccess|array $array, string|int|float $key): bool ``` @@ -359,7 +359,7 @@ public static exists(\ArrayAccess|array $array, string|int $key): bool | Parameter | Type | Description | |-----------|------|-------------| | `$array` | **\ArrayAccess|array** | | -| `$key` | **string|int** | | +| `$key` | **string|int|float** | | @@ -431,7 +431,7 @@ public static first(iterable $array, callable|null $callback = null, mixed $defa Flatten a multi-dimensional array into a single level. ```php -public static flatten(iterable $array, int $depth = INF): array +public static flatten(iterable $array, int $depth = PHP_INT_MAX): array ``` Typical use case is to flatten arrays like those returned by `get_post_meta( $id )`. @@ -490,7 +490,7 @@ public static forget(array& $array, array|string|int|float $keys): void Find a value inside of an array or object, including one nested a few levels deep. ```php -public static get(array|object|mixed $variable, array|string|int $indexes, mixed $default = null): mixed +public static get(array|object|mixed $variable, array|string|int|null $indexes, mixed $default = null): mixed ``` Example: get( $a, [ 0, 1, 2 ] ) returns the value of $a[0][1][2] or the default. @@ -505,7 +505,7 @@ Example: get( $a, [ 0, 1, 2 ] ) returns the value of $a[0][1][2] or the default. | Parameter | Type | Description | |-----------|------|-------------| | `$variable` | **array|object|mixed** | Array or object to search within. | -| `$indexes` | **array|string|int** | Specify each nested index in order.
Example: array( 'lvl1', 'lvl2' ); | +| `$indexes` | **array|string|int|null** | Specify each nested index in order.
Example: array( 'lvl1', 'lvl2' ); | | `$default` | **mixed** | Default value if the search finds nothing. | @@ -586,7 +586,7 @@ The value of the specified index or the default if not found. Check if an item or items exist in an array using "dot" notation. ```php -public static has(\ArrayAccess|array $array, array|string|int $indexes): bool +public static has(\ArrayAccess|array $array, array|string|int|null $indexes): bool ``` @@ -601,7 +601,7 @@ public static has(\ArrayAccess|array $array, array|string|int $indexes): bool | Parameter | Type | Description | |-----------|------|-------------| | `$array` | **\ArrayAccess|array** | | -| `$indexes` | **array|string|int** | The indexes to search; in order the function will look from the first to the last. | +| `$indexes` | **array|string|int|null** | The indexes to search; in order the function will look from the first to the last. | @@ -1243,6 +1243,59 @@ public static shuffle(array $array, int|null $seed = null): array +*** + +### sort_by_priority + +Sort based on Priority + +```php +public static sort_by_priority(array $array): int +``` + + + +* This method is **static**. + + + + +**Parameters:** + +| Parameter | Type | Description | +|-----------|------|-------------| +| `$array` | **array** | Array to sort. | + + + + +*** + +### sort_by_priority_comparison + +Sort based on Priority + +```php +protected static sort_by_priority_comparison(object|array $a, object|array $b): int +``` + + + +* This method is **static**. + + + + +**Parameters:** + +| Parameter | Type | Description | +|-----------|------|-------------| +| `$a` | **object|array** | First Subject to compare | +| `$b` | **object|array** | Second subject to compare | + + + + *** ### sort_recursive diff --git a/src/Arrays/Arr.php b/src/Arrays/Arr.php index 6dd829f..519ea4d 100644 --- a/src/Arrays/Arr.php +++ b/src/Arrays/Arr.php @@ -1131,6 +1131,60 @@ public static function shuffle( $array, $seed = null ) { return $array; } + /** + * Sort based on Priority + * + * @since 1.0.0 + * + * @param array $array Array to sort. + * + * @return int + */ + public static function sort_by_priority( $array ): array { + if ( ! is_array( $array ) ) { + return $array; + } + + if ( static::is_assoc( $array ) ) { + uasort( $array, [ static::class, 'sort_by_priority_comparison' ] ); + } else { + usort( $array, [ static::class, 'sort_by_priority_comparison' ] ); + } + + return $array; + } + + /** + * Sort based on Priority + * + * @since 1.0.0 + * + * @param object|array $b Second subject to compare + * + * @param object|array $a First Subject to compare + * + * @return int + */ + protected static function sort_by_priority_comparison( $a, $b ): int { + if ( is_array( $a ) ) { + $a_priority = $a['priority']; + } else { + $a_priority = $a->priority; + } + + if ( is_array( $b ) ) { + $b_priority = $b['priority']; + } else { + $b_priority = $b->priority; + } + + if ( (int) $a_priority === (int) $b_priority ) { + return 0; + } + + return (int) $a_priority > (int) $b_priority ? 1 : -1; + } + /** * Recursively sort an array by keys and values. *