PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/html/blog/wp-content/plugins/seo-ultimate/includes/jlfunctions/arr.php

https://github.com/jimmytidey/jimmytidey.co.uk
PHP | 223 lines | 111 code | 23 blank | 89 comment | 25 complexity | b839a4704d5acd3918671ed520d77fbd MD5 | raw file
  1. <?php
  2. /*
  3. JLFunctions Array Class
  4. Copyright (c)2009-2012 John Lamansky
  5. */
  6. class suarr {
  7. /**
  8. * Plugs an array's keys and/or values into sprintf-style format string(s).
  9. *
  10. * @param string|false $keyformat The sprintf-style format for the key, e.g. "prefix_%s" or "%s_suffix"
  11. * @param string|false $valueformat The sprintf-style format for the value.
  12. * @param array $array The array whose keys/values should be formatted.
  13. * @return array The array with the key/value formats applied.
  14. */
  15. static function aprintf($keyformat, $valueformat, $array) {
  16. $newarray = array();
  17. foreach ($array as $key => $value) {
  18. if ($keyformat) {
  19. if (is_int($key)) $key = $value;
  20. $key = str_replace('%s', $key, $keyformat);
  21. }
  22. if ($valueformat) $value = str_replace('%s', $value, $valueformat);
  23. $newarray[$key] = $value;
  24. }
  25. return $newarray;
  26. }
  27. /**
  28. * Removes a value from the array if found.
  29. *
  30. * @param array $array Passed by reference.
  31. * @param mixed $value The value to remove.
  32. */
  33. static function remove_value(&$array, $value) {
  34. $index = array_search($value, $array);
  35. if ($index !== false)
  36. unset($array[$index]);
  37. }
  38. /**
  39. * Turns a string into an array, with one line per array element.
  40. *
  41. * @param string $lines
  42. *
  43. * @return array
  44. */
  45. static function explode_lines($lines) {
  46. $lines = explode("\n", $lines);
  47. $lines = array_map('trim', $lines); //Remove any \r's
  48. return $lines;
  49. }
  50. /**
  51. * Sorts an array of arrays by using a given array key to locate string values in the second-level arrays and sorting the first-level array accordingly.
  52. * Alphabetical sorting is used.
  53. *
  54. * @param array $arr Passed by reference
  55. * @param string $valuekey The array key used to access the string values by which the arrays in $arr are to be sorted
  56. */
  57. static function vksort(&$arr, $valuekey) {
  58. $valuekey = sustr::preg_filter('A-Za-z0-9 ', $valuekey);
  59. uasort($arr, create_function('$a,$b', 'return strcasecmp($a["'.$valuekey.'"], $b["'.$valuekey.'"]);'));
  60. }
  61. /**
  62. * Sorts an array of arrays by using a given array key to locate string values in the second-level arrays and sorting the first-level array accordingly.
  63. * Reverse length sorting is used. (Longest strings are first, shortest strings last.)
  64. *
  65. * @param array $arr Passed by reference
  66. * @param string $valuekey The array key used to access the string values by which the arrays in $arr are to be sorted
  67. */
  68. static function vklrsort(&$arr, $valuekey) {
  69. $valuekey = sustr::preg_filter('A-Za-z0-9 ', $valuekey);
  70. uasort($arr, create_function('$a,$b', 'return strlen($b["'.$valuekey.'"]) - strlen($a["'.$valuekey.'"]);'));
  71. }
  72. /**
  73. * Flattens a multidimensional array (or an array of objects) into a single-dimensional array by discarding all but one element of the higher-level array(s).
  74. * Works like WordPress's wp_list_pluck(), except this function is recursive and supports inserting a default value if a subarray doesn't have the specified key.
  75. *
  76. * Usage example:
  77. * ---
  78. * $post_types = get_post_types(array('public' => true), 'objects');
  79. * $singular_names = suarr::flatten_values($post_types, array('labels', 'singular_name'));
  80. * ---
  81. *
  82. * @param array $arr The multidimensional array (or array of objects) to flatten
  83. * @param array|string|int $value_keys Each array/object in $arr will be replaced with its element/property named $value_keys. If $value_keys is an array, this will be done recursively.
  84. * @param bool $use_default_if_empty If a given array/object in $arr does not have an element/property named $value_keys, should a default value be inserted?
  85. * @param mixed $default
  86. *
  87. * @return array The flattened array
  88. */
  89. static function flatten_values($arr, $value_keys, $use_default_if_empty=false, $default='') {
  90. foreach ((array)$value_keys as $key)
  91. $arr = suarr::_flatten_values($arr, $key, $use_default_if_empty, $default);
  92. return $arr;
  93. }
  94. static function _flatten_values($arr, $value_key = 0, $use_default_if_empty=false, $default='') {
  95. if (!is_array($arr) || !count($arr)) return array();
  96. $newarr = array();
  97. foreach ($arr as $key => $array_value) {
  98. $success = false;
  99. if (is_array($array_value)) {
  100. if (isset($array_value[$value_key])) {
  101. $newarr[$key] = $array_value[$value_key];
  102. $success = true;
  103. }
  104. } elseif (is_object($array_value)) {
  105. if (isset($array_value->$value_key)) {
  106. $newarr[$key] = $array_value->$value_key;
  107. $success = true;
  108. }
  109. }
  110. if (!$success && $use_default_if_empty)
  111. $newarr[$key] = $default;
  112. }
  113. return $newarr;
  114. }
  115. /**
  116. * Renames keys in an array. Supports recursion.
  117. *
  118. * @param array $array The array whose keys should be renamed
  119. * @param array $key_changes An array of (old_key => new_key)
  120. * @param bool $recursive Whether or not to do the same for subarrays of $array
  121. * @param bool $return_replaced_only If true, then elements whose keys were *not* renamed will be discarded
  122. *
  123. * @return array
  124. */
  125. static function key_replace($array, $key_changes, $recursive = true, $return_replaced_only = false) {
  126. $newarray = array();
  127. foreach ($array as $key => $value) {
  128. $changed = false;
  129. if ($recursive && is_array($value)) {
  130. $oldvalue = $value;
  131. $value = suarr::key_replace($value, $key_changes, true, $return_replaced_only);
  132. if ($oldvalue != $value) $changed = true;
  133. }
  134. if (isset($key_changes[$key])) {
  135. $key = $key_changes[$key];
  136. $changed = true;
  137. }
  138. if ($changed || !$return_replaced_only)
  139. $newarray[$key] = $value;
  140. }
  141. return $newarray;
  142. }
  143. /**
  144. * Runs a find/replace operation on values in an array.
  145. *
  146. * @param array $array The array whose values should be replaced
  147. * @param array $value_changes An array of (old_value => new_value)
  148. * @param bool $recursive Whether or not to do the same for subarrays of $array
  149. * @param bool $return_replaced_only If true, then elements *not* replaced will be discarded
  150. *
  151. * @return array
  152. */
  153. static function value_replace($array, $value_changes, $recursive = true, $return_replaced_only = false) {
  154. $newarray = array();
  155. foreach ((array)$array as $key => $value) {
  156. $oldvalue = $value;
  157. if ($recursive && is_array($value))
  158. $value = suarr::value_replace($value, $value_changes, true);
  159. elseif (isset($value_changes[$value]))
  160. $value = $value_changes[$value];
  161. if ($value != $oldvalue || !$return_replaced_only)
  162. $newarray[$key] = $value;
  163. }
  164. return $newarray;
  165. }
  166. /**
  167. * Goes through an array of arrays/objects, plucks two elements/properties from each array/object, and constructs a new array, with one element/property as the key, and the other as the value.
  168. *
  169. * @param array $arr The array to run this process on.
  170. * @param array|string|int $keyloc The location (either a string/integer key, or an array of nested keys) of the elements' values to be used as keys in the new array
  171. * @param array|string|int $valloc The location (either a string/integer key, or an array of nested keys) of the elements' values to be used as values in the new array
  172. * @param bool $use_default_if_empty Whether or not to use a default value in the event that nothing is located at $keyloc or $valloc for a given array/object in $arr
  173. * @param mixed $default
  174. *
  175. * @return array
  176. */
  177. static function simplify($arr, $keyloc, $valloc, $use_default_if_empty=false, $default='') {
  178. $keys = suarr::flatten_values($arr, $keyloc, $use_default_if_empty, $default);
  179. $values = suarr::flatten_values($arr, $valloc, $use_default_if_empty, $default);
  180. return array_combine($keys, $values);
  181. }
  182. //Function based on http://php.net/manual/en/function.array-unique.php#82508
  183. static function in_array_i($str, $a) {
  184. foreach($a as $v){
  185. if (strcasecmp($str, $v)==0)
  186. return true;
  187. }
  188. return false;
  189. }
  190. //Function based on http://php.net/manual/en/function.array-unique.php#82508
  191. static function array_unique_i($a) {
  192. $n = array();
  193. foreach($a as $k=>$v) {
  194. if (!suarr::in_array_i($v, $n))
  195. $n[$k] = $v;
  196. }
  197. return $n;
  198. }
  199. }
  200. ?>