Form
5 years ago
Frontend
5 years ago
Gateways
5 years ago
ArrayDataSet.php
6 years ago
Hooks.php
5 years ago
Utils.php
6 years ago
ArrayDataSet.php
65 lines
| 1 | <?php |
| 2 | namespace Give\Helpers; |
| 3 | |
| 4 | class ArrayDataSet { |
| 5 | /** |
| 6 | * This function will return array with renamed keys. |
| 7 | * |
| 8 | * This function only support one dimensional array. |
| 9 | * You can pass a multi dimensional array but only zero level array keys will be renamed. |
| 10 | * |
| 11 | * @param array $array |
| 12 | * @param array $renameTo Pass array of existing key name as key and new key name as value. |
| 13 | * |
| 14 | * @return array |
| 15 | * @since 2.7.0 |
| 16 | */ |
| 17 | public static function renameKeys( $array, $renameTo ) { |
| 18 | // Rename key if property name exist for them. |
| 19 | foreach ( $renameTo as $oldKey => $newKey ) { |
| 20 | if ( array_key_exists( $oldKey, $array ) ) { |
| 21 | $array[ $newKey ] = $array[ $oldKey ]; |
| 22 | unset( $array[ $oldKey ] ); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | return $array; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Return whether or not array contains required keys. |
| 31 | * |
| 32 | * This function only support one dimensional array. |
| 33 | * |
| 34 | * @since 2.7.0 |
| 35 | * |
| 36 | * @param array $array |
| 37 | * @param array $requiredKeys Array of required keys. |
| 38 | * |
| 39 | * @return bool |
| 40 | */ |
| 41 | public static function hasRequiredKeys( $array, $requiredKeys ) { |
| 42 | return (bool) array_intersect_key( $array, array_flip( $requiredKeys ) ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Return array with grouped under specific key. |
| 47 | * |
| 48 | * @param array $array |
| 49 | * @param array $itemsToMove |
| 50 | * @param string $arrayKey |
| 51 | * |
| 52 | * @return mixed |
| 53 | */ |
| 54 | public static function moveArrayItemsUnderArrayKey( $array, $itemsToMove, $arrayKey ) { |
| 55 | foreach ( $itemsToMove as $key ) { |
| 56 | if ( array_key_exists( $key, $array ) ) { |
| 57 | $array[ $arrayKey ][ $key ] = $array[ $key ]; |
| 58 | unset( $array[ $key ] ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return $array; |
| 63 | } |
| 64 | } |
| 65 |