TheLib_Array

The Array component.

Defined (1)

The class is defined in the following location(s).

/lib/wpmu-lib/inc/class-thelib-array.php  
  1. class TheLib_Array extends TheLib { 
  2.  
  3. /** 
  4. * If the specified variable is an array it will be returned. Otherwise 
  5. * an empty array is returned. 
  6. * @since 1.0.14 
  7. * @api 
  8. * @param mixed $val1 Value that maybe is an array. 
  9. * @param mixed $val2 Optional, Second value that maybe is an array. 
  10. * @return array 
  11. */ 
  12. public function get( &$val1, $val2 = array() ) { 
  13. if ( is_array( $val1 ) ) { 
  14. return $val1; 
  15. } else if ( is_array( $val2 ) ) { 
  16. return $val2; 
  17. } else { 
  18. return array(); 
  19.  
  20. /** 
  21. * Inserts any number of scalars or arrays at the point 
  22. * in the haystack immediately after the search key ($needle) was found,  
  23. * or at the end if the needle is not found or not supplied. 
  24. * Modifies $haystack in place. 
  25. * @since 1.1.3 
  26. * @api 
  27. * @param array &$haystack The associative array to search. This will be modified by the function 
  28. * @param string $where [after|before] 
  29. * @param string $needle The key to search for 
  30. * @param mixed $stuff One or more arrays or scalars to be inserted into $haystack 
  31. * @return int The index at which $needle was found 
  32. */ 
  33. public function insert( &$haystack, $where, $needle, $stuff ) { 
  34. if ( ! is_array( $haystack ) ) { return $haystack; } 
  35.  
  36. $new_array = array(); 
  37. for ( $i = 3; $i < func_num_args(); ++$i ) { 
  38. $arg = func_get_arg( $i ); 
  39. if ( is_array( $arg ) ) { 
  40. $new_array = array_merge( $new_array, $arg ); 
  41. } else { 
  42. $new_array[] = $arg; 
  43.  
  44. $i = 0; 
  45. foreach ( $haystack as $key => $value ) { 
  46. $i += 1; 
  47.  
  48. if ( $key == $needle ) { 
  49. if ( 'before' == $where ) { 
  50. $i -= 1; 
  51.  
  52. break; 
  53.  
  54. $haystack = array_merge( 
  55. array_slice( $haystack, 0, $i, true ),  
  56. $new_array,  
  57. array_slice( $haystack, $i, null, true ) 
  58. ); 
  59.  
  60. return $i; 
  61.  
  62. /** 
  63. * Tests if the given array is sequential or associative. 
  64. * It is considered sequential, when all array keys are integers. 
  65. * Otherwise the result is false (meaning: associative array) 
  66. * @since 1.1.3 
  67. * @api 
  68. * @param array $array 
  69. * @return bool 
  70. */ 
  71. public function is_seq( $array ) { 
  72. for ( 
  73. reset( $array ); 
  74. is_int( key( $array ) ); 
  75. next( $array ) 
  76. ) {} 
  77. return is_null( key( $array ) ); 
  78.  
  79. /** 
  80. * array_merge_recursive does indeed merge arrays, but it converts values with duplicate 
  81. * keys to arrays rather than overwriting the value in the first array with the duplicate 
  82. * value in the second array, as array_merge does. I.e., with array_merge_recursive,  
  83. * this happens (documented behavior): 
  84. * array_merge_recursive(array('key' => 'org value'), array('key' => 'new value')); 
  85. * => array('key' => array('org value', 'new value')); 
  86. * merge_recursive_distinct does not change the datatypes of the values in the arrays. 
  87. * Matching keys' values in the second array overwrite those in the first array, as is the 
  88. * case with array_merge, i.e.: 
  89. * merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value')); 
  90. * => array('key' => array('new value')); 
  91. * Parameters are passed by reference, though only for performance reasons. They're not 
  92. * altered by this function. 
  93. * @since 1.1.2 
  94. * @api 
  95. * @param array $array1 
  96. * @param array $array2 
  97. * @return array 
  98. * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk> 
  99. * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com> 
  100. */ 
  101. public function merge_recursive_distinct( array &$array1, array &$array2 ) { 
  102. $merged = $array1; 
  103.  
  104. foreach ( $array2 as $key => &$value ) { 
  105. if ( is_array( $value ) && isset( $merged[$key] ) && is_array( $merged[$key] ) ) { 
  106. if ( $this->is_seq( $value ) && $this->is_seq( $merged[$key] ) ) { 
  107. $merged[$key] = array_merge( $merged[$key], $value ); 
  108. } else { 
  109. $merged[$key] = $this->merge_recursive_distinct( $merged[$key], $value ); 
  110. } else { 
  111. $merged[$key] = $value; 
  112.  
  113. return $merged; 
  114.  
  115. /** 
  116. * Checks if the given array contains all the specified fields. 
  117. * If fields are not defined then they will be added to the source array 
  118. * with the boolean value false. 
  119. * This function is used to initialize optional fields. 
  120. * It is optimized and tested to yield best performance. 
  121. * @since 1.0.14 
  122. * @api 
  123. * @param Array|Object $arr The array or object to check. 
  124. * @param strings|Array $fields List of fields to check for. 
  125. * @return int Number of missing fields that were initialized. 
  126. */ 
  127. public function equip( &$arr, $fields ) { 
  128. $missing = 0; 
  129. $is_obj = false; 
  130.  
  131. if ( is_object( $arr ) ) { $is_obj = true; } 
  132. else if ( ! is_array( $arr ) ) { return -1; } 
  133.  
  134. if ( ! is_array( $fields ) ) { 
  135. $fields = func_get_args(); 
  136. array_shift( $fields ); // Remove $arr from the field list. 
  137.  
  138. foreach ( $fields as $field ) { 
  139. if ( $is_obj ) { 
  140. if ( ! property_exists( $arr, $field ) ) { 
  141. $arr->$field = false; 
  142. $missing += 1; 
  143. } else { 
  144. if ( ! isset( $arr[ $field ] ) ) { 
  145. $arr[ $field ] = false; 
  146. $missing += 1; 
  147.  
  148. return $missing; 
  149.  
  150. /** 
  151. * Short function for lib3()->equip( $_POST, ... ) 
  152. * @since 1.0.14 
  153. * @api 
  154. * @uses equip() 
  155. * @param strings|Array <param list> 
  156. * @return int Number of missing fields that were initialized. 
  157. */ 
  158. public function equip_post( $fields ) { 
  159. $fields = is_array( $fields ) ? $fields : func_get_args(); 
  160. return $this->equip( $_POST, $fields ); 
  161.  
  162. /** 
  163. * Short function for lib3()->equip( $_REQUEST, ... ) 
  164. * @since 1.0.14 
  165. * @api 
  166. * @uses equip() 
  167. * @param strings|Array <param list> 
  168. * @return int Number of missing fields that were initialized. 
  169. */ 
  170. public function equip_request( $fields ) { 
  171. $fields = is_array( $fields ) ? $fields : func_get_args(); 
  172. return $this->equip( $_REQUEST, $fields ); 
  173.  
  174. /** 
  175. * Short function for lib3()->equip( $_GET, ... ) 
  176. * @since 1.1.3 
  177. * @api 
  178. * @uses equip() 
  179. * @param strings|Array <param list> 
  180. * @return int Number of missing fields that were initialized. 
  181. */ 
  182. public function equip_get( $fields ) { 
  183. $fields = is_array( $fields ) ? $fields : func_get_args(); 
  184. return $this->equip( $_GET, $fields ); 
  185.  
  186. /** 
  187. * By default WordPress escapes all GPC values with slashes. 
  188. * {@see wp-includes/load.php wp_magic_quotes()} 
  189. * This function can be used to strip slashes of a list of parameters. This 
  190. * ensures that not all parameters are un-escaped but only ones that are 
  191. * used by the current function. 
  192. * @since 1.1.4 
  193. * @api 
  194. * @param Array $arr The array or object to check. 
  195. * @param strings|Array $fields List of fields to check for. 
  196. * @return int Number of fields that were un-escaped. 
  197. */ 
  198. public function strip_slashes( &$arr, $fields ) { 
  199. $modified = 0; 
  200. if ( ! is_array( $arr ) ) { return -1; } 
  201.  
  202. if ( ! is_array( $fields ) ) { 
  203. $fields = func_get_args(); 
  204. array_shift( $fields ); // Remove $arr from the field list. 
  205.  
  206. foreach ( $fields as $field ) { 
  207. if ( isset( $arr[ $field ] ) ) { 
  208. $arr[ $field ] = stripslashes_deep( $arr[$field] ); 
  209. $modified += 1; 
  210.  
  211. return $modified; 
  212.