Brak opisu

functions-list.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Helper functions to work with multidimensional arrays easier.
  4. *
  5. * @since 1.5.6
  6. */
  7. /**
  8. * Determine whether the given value is array accessible.
  9. *
  10. * @since 1.5.6
  11. *
  12. * @param mixed $value Checkin to accessible.
  13. *
  14. * @return bool
  15. */
  16. function wpforms_list_accessible( $value ) {
  17. return is_array( $value ) || $value instanceof ArrayAccess;
  18. }
  19. /**
  20. * Set an array item to a given value using "dot" notation.
  21. *
  22. * If no key is given to the method, the entire array will be replaced.
  23. *
  24. * @since 1.5.6
  25. *
  26. * @param array $array Existing array.
  27. * @param string $key Path to set.
  28. * @param mixed $value Value to set.
  29. * @param string $separator Separator.
  30. *
  31. * @return array New array.
  32. */
  33. function wpforms_list_set( $array, $key, $value, $separator = '.' ) {
  34. if ( ! wpforms_list_accessible( $array ) ) {
  35. return $value;
  36. }
  37. if ( is_null( $key ) ) {
  38. $array = $value;
  39. return $array;
  40. }
  41. $keys = explode( $separator, $key );
  42. $count_keys = count( $keys );
  43. $values = array_values( $keys );
  44. $last_key = $values[ $count_keys - 1 ];
  45. $tmp_array = &$array;
  46. for ( $i = 0; $i < $count_keys - 1; $i ++ ) {
  47. $k = $keys[ $i ];
  48. $tmp_array = &$tmp_array[ $k ];
  49. }
  50. $tmp_array[ $last_key ] = $value;
  51. return $array;
  52. }
  53. /**
  54. * Determine if the given key exists in the provided array.
  55. *
  56. * @since 1.5.6
  57. *
  58. * @param \ArrayAccess|array $array Existing array.
  59. * @param string|int $key To check.
  60. *
  61. * @return bool
  62. */
  63. function wpforms_list_exists( $array, $key ) {
  64. if ( ! wpforms_list_accessible( $array ) ) {
  65. return false;
  66. }
  67. if ( $array instanceof ArrayAccess ) {
  68. return $array->offsetExists( $key );
  69. }
  70. return array_key_exists( $key, $array );
  71. }
  72. /**
  73. * Get an item from an array using "dot" notation.
  74. *
  75. * @since 1.5.6
  76. *
  77. * @param \ArrayAccess|array $array Where we want to get.
  78. * @param string $key Key with dot's.
  79. * @param mixed $default Value.
  80. *
  81. * @return mixed
  82. */
  83. function wpforms_list_get( $array, $key, $default = null ) {
  84. if ( ! wpforms_list_accessible( $array ) ) {
  85. return $default;
  86. }
  87. if ( is_null( $key ) ) {
  88. return $array;
  89. }
  90. if ( ! is_string( $key ) ) {
  91. return $default;
  92. }
  93. if ( wpforms_list_exists( $array, $key ) ) {
  94. return $array[ $key ];
  95. }
  96. foreach ( explode( '.', $key ) as $segment ) {
  97. if ( wpforms_list_accessible( $array ) && wpforms_list_exists( $array, $segment ) ) {
  98. $array = $array[ $segment ];
  99. } else {
  100. return $default;
  101. }
  102. }
  103. return $array;
  104. }
  105. /**
  106. * Check if an item exists in an array using "dot" notation.
  107. *
  108. * @since 1.5.6
  109. *
  110. * @param \ArrayAccess|array $array To check.
  111. * @param string $key Keys with dot's.
  112. *
  113. * @return bool
  114. */
  115. function wpforms_list_has( $array, $key ) {
  116. if ( ! $array ) {
  117. return false;
  118. }
  119. if ( is_null( $key ) || ! is_string( $key ) ) {
  120. return false;
  121. }
  122. if ( wpforms_list_exists( $array, $key ) ) {
  123. return true;
  124. }
  125. foreach ( explode( '.', $key ) as $segment ) {
  126. if ( wpforms_list_accessible( $array ) && wpforms_list_exists( $array, $segment ) ) {
  127. $array = $array[ $segment ];
  128. } else {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * Determine if an array is associative.
  136. *
  137. * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
  138. *
  139. * @since 1.5.6
  140. *
  141. * @param array $array To check.
  142. *
  143. * @return bool
  144. */
  145. function wpforms_list_is_assoc( $array ) {
  146. $keys = array_keys( $array );
  147. return array_keys( $keys ) !== $keys;
  148. }
  149. /**
  150. * Get a subset of the items from the given array.
  151. *
  152. * @since 1.5.6
  153. *
  154. * @param array $array To get.
  155. * @param array|string $keys To filter.
  156. *
  157. * @return array
  158. */
  159. function wpforms_list_only( $array, $keys ) {
  160. return array_intersect_key( $array, array_flip( (array) $keys ) );
  161. }
  162. /**
  163. * Remove one or many array items from a given array using "dot" notation.
  164. *
  165. * @since 1.5.6
  166. *
  167. * @param array $array To forget.
  168. * @param array|string $keys To exclude.
  169. *
  170. * @return array
  171. */
  172. function wpforms_list_forget( $array, $keys ) {
  173. $tmp_array = &$array;
  174. $keys = (array) $keys;
  175. if ( count( $keys ) === 0 ) {
  176. return $array;
  177. }
  178. foreach ( $keys as $key ) {
  179. // if the exact key exists in the top-level, remove it.
  180. if ( wpforms_list_exists( $array, $key ) ) {
  181. unset( $array[ $key ] );
  182. continue;
  183. }
  184. $parts = explode( '.', $key );
  185. $count_keys = count( $parts );
  186. $values = array_values( $parts );
  187. $last_key = $values[ $count_keys - 1 ];
  188. for ( $i = 0; $i < $count_keys - 1; $i ++ ) {
  189. $k = $parts[ $i ];
  190. $tmp_array = &$tmp_array[ $k ];
  191. }
  192. unset( $tmp_array[ $last_key ] );
  193. }
  194. return $array;
  195. }
  196. /**
  197. * Insert a value or key/value pair after a specific key in an array.
  198. * If key doesn't exist, value is appended to the end of the array.
  199. *
  200. * @since 1.5.8
  201. *
  202. * @param array $array Array where to insert.
  203. * @param string $key Insert after key.
  204. * @param array $new Array to insert.
  205. *
  206. * @return array
  207. */
  208. function wpforms_list_insert_after( $array, $key, $new ) {
  209. $keys = array_keys( $array );
  210. $index = array_search( $key, $keys, true );
  211. $pos = false === $index ? count( $array ) : $index + 1;
  212. return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
  213. }