Skip to content

Commit

Permalink
Add a couple of other methods
Browse files Browse the repository at this point in the history
  • Loading branch information
borkweb committed Aug 25, 2023
1 parent b959844 commit 8584a9a
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 26 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ A library for array manipulations.
* [get](/docs/classes/StellarWP/Arrays/Array_Utils.md#get)
* [get_first_set](/docs/classes/StellarWP/Arrays/Array_Utils.md#get_first_set)
* [get_in_any](/docs/classes/StellarWP/Arrays/Array_Utils.md#get_in_any)
* [insert_after_key](/docs/classes/StellarWP/Arrays/Array_Utils.md#insert_after_key)
* [insert_before_key](/docs/classes/StellarWP/Arrays/Array_Utils.md#insert_before_key)
* [list_to_array](/docs/classes/StellarWP/Arrays/Array_Utils.md#list_to_array)
* [map_or_discard](/docs/classes/StellarWP/Arrays/Array_Utils.md#map_or_discard)
* [merge_recursive](/docs/classes/StellarWP/Arrays/Array_Utils.md#merge_recursive)
* [merge_recursive_query_vars](/docs/classes/StellarWP/Arrays/Array_Utils.md#merge_recursive_query_vars)
* [parse_associative_array_alias](/docs/classes/StellarWP/Arrays/Array_Utils.md#parse_associative_array_alias)
* [recursive_ksort](/docs/classes/StellarWP/Arrays/Array_Utils.md#recursive_ksort)
Expand Down
115 changes: 89 additions & 26 deletions docs/classes/StellarWP/Arrays/Array_Utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,7 @@ Array utilities
* Full name: `\StellarWP\Arrays\Array_Utils`


* [add_prefixed_keys_to](#add_prefixed_keys_to)
* [add_unprefixed_keys_to](#add_unprefixed_keys_to)
* [array_visit_recursive](#array_visit_recursive)
* [destringify_keys](#destringify_keys)
* [escape_multidimensional_array](#escape_multidimensional_array)
* [filter_to_flat_scalar_associative_array](#filter_to_flat_scalar_associative_array)
* [filter_prefixed](#filter_prefixed)
* [flatten](#flatten)
* [get](#get)
* [get_first_set](#get_first_set)
* [get_in_any](#get_in_any)
* [list_to_array](#list_to_array)
* [map_or_discard](#map_or_discard)
* [merge_recursive_query_vars](#merge_recursive_query_vars)
* [parse_associative_array_alias](#parse_associative_array_alias)
* [recursive_ksort](#recursive_ksort)
* [remove_numeric_keys_recursive](#remove_numeric_keys_recursive)
* [remove_string_keys_recursive](#remove_string_keys_recursive)
* [set](#set)
* [shape_filter](#shape_filter)
* [stringify_keys](#stringify_keys)
* [strpos](#strpos)
* [to_list](#to_list)
* [usearch](#usearch)


## Methods

Expand Down Expand Up @@ -190,7 +167,7 @@ The sanitized array

**See Also:**

* https://gist.github.com/esthezia/5804445 -
* https://gist.github.com/esthezia/5804445 -

***

Expand Down Expand Up @@ -386,6 +363,62 @@ The value of the specified index or the default if not found.



***

### insert_after_key

Insert an array after a specified key within another array.

```php
public static insert_after_key(string|int $key, array $source_array, mixed $insert): array
```



* This method is **static**.




**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `$key` | **string|int** | The key of the array to insert after. |
| `$source_array` | **array** | The array to insert into. |
| `$insert` | **mixed** | Value or array to insert. |




***

### insert_before_key

Insert an array immediately before a specified key within another array.

```php
public static insert_before_key(string|int $key, array $source_array, mixed $insert): array
```



* This method is **static**.




**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `$key` | **string|int** | The key of the array to insert before. |
| `$source_array` | **array** | The array to insert into. |
| `$insert` | **mixed** | Value or array to insert. |




***

### list_to_array
Expand Down Expand Up @@ -453,6 +486,36 @@ not be mapped.



***

### merge_recursive

Recursively merge two arrays preserving keys.

```php
public merge_recursive(array& $array1, array& $array2): array
```








**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `$array1` | **array** | |
| `$array2` | **array** | |



**See Also:**

* http://php.net/manual/en/function.array-merge-recursive.php#92195 -

***

### merge_recursive_query_vars
Expand Down Expand Up @@ -729,7 +792,7 @@ Integer position of first needle occurrence.

**See Also:**

* \StellarWP\Arrays\strpos() -
* \StellarWP\Arrays\strpos() -

***

Expand Down
76 changes: 76 additions & 0 deletions src/Arrays/Array_Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,56 @@ public static function get_in_any( array $variables, $indexes, $default = null )
return $default;
}

/**
* Insert an array after a specified key within another array.
*
* @param string|int $key The key of the array to insert after.
* @param array $source_array The array to insert into.
* @param mixed $insert Value or array to insert.
*
* @return array
*/
public static function insert_after_key( $key, array $source_array, $insert ): array {
if ( ! is_array( $insert ) ) {
$insert = [ $insert ];
}

if ( array_key_exists( $key, $source_array ) ) {
$position = array_search( $key, array_keys( $source_array ) ) + 1;
$source_array = array_slice( $source_array, 0, $position, true ) + $insert + array_slice( $source_array, $position, null, true );
} else {
// If no key is found, then add it to the end of the array.
$source_array += $insert;
}

return $source_array;
}

/**
* Insert an array immediately before a specified key within another array.
*
* @param string|int $key The key of the array to insert before.
* @param array $source_array The array to insert into.
* @param mixed $insert Value or array to insert.
*
* @return array
*/
public static function insert_before_key( $key, array $source_array, $insert ): array {
if ( ! is_array( $insert ) ) {
$insert = [ $insert ];
}

if ( array_key_exists( $key, $source_array ) ) {
$position = array_search( $key, array_keys( $source_array ) );
$source_array = array_slice( $source_array, 0, $position, true ) + $insert + array_slice( $source_array, $position, null, true );
} else {
// If no key is found, then add it to the end of the array.
$source_array += $insert;
}

return $source_array;
}

/**
* Converts a list to an array filtering out empty string elements.
*
Expand Down Expand Up @@ -429,6 +479,32 @@ public static function map_or_discard( $keys, array $map, bool &$found = true )
return $found ? $mapped[0] : false;
}

/**
* Recursively merge two arrays preserving keys.
*
* @since 1.0.0
*
* @link http://php.net/manual/en/function.array-merge-recursive.php#92195
*
* @param array $array1
* @param array $array2
*
* @return array
*/
function merge_recursive( array &$array1, array &$array2 ): array {
$merged = $array1;

foreach ( $array2 as $key => &$value ) {
if ( is_array( $value ) && isset( $merged [ $key ] ) && is_array( $merged [ $key ] ) ) {
$merged [ $key ] = static::merge_recursive( $merged [ $key ], $value );
} else {
$merged [ $key ] = $value;
}
}

return $merged;
}

/**
* Merges two or more arrays in the nested format used by WP_Query arguments preserving and merging them correctly.
*
Expand Down

0 comments on commit 8584a9a

Please sign in to comment.