Blocks
4 months ago
Contracts
9 months ago
Errors
3 weeks ago
Scripts
1 month ago
Arrays.php
3 years ago
ColorService.php
3 years ago
Currency.php
1 year ago
Encryption.php
3 years ago
Interval.php
6 months ago
Server.php
2 years ago
TimeDate.php
2 months ago
Translations.php
3 years ago
URL.php
2 years ago
UtilityService.php
3 years ago
UtilityServiceProvider.php
3 years ago
kses.json
5 hours ago
Arrays.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Support; |
| 4 | |
| 5 | /** |
| 6 | * Array support. |
| 7 | */ |
| 8 | class Arrays { |
| 9 | /** |
| 10 | * Flatten an array |
| 11 | * |
| 12 | * @param array $array Array. |
| 13 | * |
| 14 | * @return array |
| 15 | */ |
| 16 | public static function flatten( array $array ) { |
| 17 | $return = array(); |
| 18 | array_walk_recursive( |
| 19 | $array, |
| 20 | function( $a ) use ( &$return ) { |
| 21 | $return[] = $a; |
| 22 | } |
| 23 | ); |
| 24 | return $return; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Insert an array after a key in another array. |
| 29 | * |
| 30 | * @param string $key Key to insert after. |
| 31 | * @param array $source_array Array to insert into. |
| 32 | * @param array $insert_array Array to insert. |
| 33 | * |
| 34 | * @throws \Exception If key does not exist in the array. |
| 35 | * |
| 36 | * @return array |
| 37 | */ |
| 38 | public static function insertAfter( $key, $source_array, $insert_array ) { |
| 39 | $keys = array_keys( $source_array ); |
| 40 | $index = array_search( $key, $keys ); |
| 41 | |
| 42 | if ( false === $index ) { |
| 43 | throw new \Exception( "Key $key does not exist in the array" ); |
| 44 | } |
| 45 | |
| 46 | $pos = $index + 1; |
| 47 | |
| 48 | return array_slice( $source_array, 0, $pos, true ) + |
| 49 | $insert_array + |
| 50 | array_slice( $source_array, $pos, null, true ); |
| 51 | } |
| 52 | } |
| 53 |