PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/sharpmachine/wakeupmedia.com
PHP | 228 lines | 162 code | 35 blank | 31 comment | 40 complexity | 2902b9248268b5ed594644cc63ea3484 MD5 | raw file
  1. <?php
  2. /*
  3. JLFunctions Array Class
  4. Copyright (c)2009-2011 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. 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 elements that are blank (after trimming) from the beginning of the given array.
  29. */
  30. function ltrim($array) {
  31. while (count($array) && !strlen(trim($array[0])))
  32. array_shift($array);
  33. return $array;
  34. }
  35. /**
  36. * Removes a value from the array if found.
  37. */
  38. function remove_value(&$array, $value) {
  39. $index = array_search($value, $array);
  40. if ($index !== false)
  41. unset($array[$index]);
  42. }
  43. /**
  44. * Returns whether or not any of the specified $needles are in the $haystack.
  45. *
  46. * @param array $needles
  47. * @param array $haystack
  48. * @param bool $case Whether or not the search should be case-sensitive.
  49. *
  50. * @return bool
  51. */
  52. function any_in_array($needles, $haystack, $case = true) {
  53. if (!$case) {
  54. $needles = array_map('strtolower', $needles);
  55. $haystack = array_map('strtolower', $haystack);
  56. }
  57. return count(array_intersect($needles, $haystack)) > 0;
  58. }
  59. function explode_lines($lines) {
  60. $lines = explode("\n", $lines);
  61. $lines = array_map('trim', $lines); //Remove any \r's
  62. return $lines;
  63. }
  64. //Based on recursive array search function from:
  65. //http://www.php.net/manual/en/function.array-search.php#91365
  66. function search_recursive($needle, $haystack) {
  67. foreach ($haystack as $key => $value) {
  68. if ($needle === $value || (is_array($value) && suarr::search_recursive($needle, $value) !== false))
  69. return $key;
  70. }
  71. return false;
  72. }
  73. function recursive_get($array, $key) {
  74. if (is_array($array)) {
  75. if (isset($array[$key]))
  76. return $array[$key];
  77. foreach ($array as $subarray) {
  78. if ($value = suarr::recursive_get($subarray, $key))
  79. return $value;
  80. }
  81. }
  82. return false;
  83. }
  84. function vksort(&$arr, $valuekey) {
  85. $valuekey = sustr::preg_filter('A-Za-z0-9 ', $valuekey);
  86. uasort($arr, create_function('$a,$b', 'return strcasecmp($a["'.$valuekey.'"], $b["'.$valuekey.'"]);'));
  87. }
  88. function vklrsort(&$arr, $valuekey) {
  89. $valuekey = sustr::preg_filter('A-Za-z0-9 ', $valuekey);
  90. uasort($arr, create_function('$a,$b', 'return strlen($b["'.$valuekey.'"]) - strlen($a["'.$valuekey.'"]);'));
  91. }
  92. function flatten_values($arr, $value_keys, $use_default_if_empty=false, $default='') {
  93. foreach ((array)$value_keys as $key)
  94. $arr = suarr::_flatten_values($arr, $key, $use_default_if_empty, $default);
  95. return $arr;
  96. }
  97. function _flatten_values($arr, $value_key = 0, $use_default_if_empty=false, $default='') {
  98. if (!is_array($arr) || !count($arr)) return array();
  99. $newarr = array();
  100. foreach ($arr as $key => $array_value) {
  101. $success = false;
  102. if (is_array($array_value)) {
  103. if (isset($array_value[$value_key])) {
  104. $newarr[$key] = $array_value[$value_key];
  105. $success = true;
  106. }
  107. } elseif (is_object($array_value)) {
  108. if (isset($array_value->$value_key)) {
  109. $newarr[$key] = $array_value->$value_key;
  110. $success = true;
  111. }
  112. }
  113. if (!$success && $use_default_if_empty)
  114. $newarr[$key] = $default;
  115. }
  116. return $newarr;
  117. }
  118. function key_replace($array, $key_changes, $recursive = true, $return_replaced_only = false) {
  119. $newarray = array();
  120. foreach ($array as $key => $value) {
  121. $changed = false;
  122. if ($recursive && is_array($value)) {
  123. $oldvalue = $value;
  124. $value = suarr::key_replace($value, $key_changes, true, $return_replaced_only);
  125. if ($oldvalue != $value) $changed = true;
  126. }
  127. if (isset($key_changes[$key])) {
  128. $key = $key_changes[$key];
  129. $changed = true;
  130. }
  131. if ($changed || !$return_replaced_only)
  132. $newarray[$key] = $value;
  133. }
  134. return $newarray;
  135. }
  136. function value_replace($array, $value_changes, $recursive = true, $return_replaced_only = false) {
  137. $newarray = array();
  138. foreach ((array)$array as $key => $value) {
  139. $oldvalue = $value;
  140. if ($recursive && is_array($value))
  141. $value = suarr::value_replace($value, $value_changes, true);
  142. elseif (isset($value_changes[$value]))
  143. $value = $value_changes[$value];
  144. if ($value != $oldvalue || !$return_replaced_only)
  145. $newarray[$key] = $value;
  146. }
  147. return $newarray;
  148. }
  149. function simplify($arr, $keyloc, $valloc, $use_default_if_empty=false, $default='') {
  150. $keys = suarr::flatten_values($arr, $keyloc, $use_default_if_empty, $default);
  151. $values = suarr::flatten_values($arr, $valloc, $use_default_if_empty, $default);
  152. return array_combine($keys, $values);
  153. }
  154. function has_keys($array, $keys) {
  155. if (is_array($array) && is_array($keys)) {
  156. if (count($keys))
  157. return count(array_diff($keys, array_keys($array))) == 0;
  158. return true;
  159. }
  160. return false;
  161. }
  162. function key_check($array, $function) {
  163. return (count($array) == count(array_filter(array_keys($array), $function)));
  164. }
  165. //Function based on http://php.net/manual/en/function.array-unique.php#82508
  166. function in_array_i($str, $a) {
  167. foreach($a as $v){
  168. if (strcasecmp($str, $v)==0)
  169. return true;
  170. }
  171. return false;
  172. }
  173. //Function based on http://php.net/manual/en/function.array-unique.php#82508
  174. function array_unique_i($a) {
  175. $n = array();
  176. foreach($a as $k=>$v) {
  177. if (!suarr::in_array_i($v, $n))
  178. $n[$k] = $v;
  179. }
  180. return $n;
  181. }
  182. function replace_empty_values_with_keys($array) {
  183. $newarray = array();
  184. foreach ($array as $key => $value) {
  185. if (empty($value))
  186. $newarray[$key] = $key;
  187. else
  188. $newarray[$key] = $value;
  189. }
  190. return $newarray;
  191. }
  192. }
  193. ?>