PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/array.php

https://github.com/lafka/wave
PHP | 128 lines | 55 code | 15 blank | 58 comment | 8 complexity | e1d6cdb6d177e8f66dec04935ecacfe0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Wave PHP
  4. *
  5. * Copyright (c) 2010 - 2011 Frengstad Web Teknologi and contributors
  6. * All rights reserved
  7. *
  8. * Library of array functions
  9. *
  10. * @package wave
  11. * @version 0.2
  12. * @copyright Frengstad Web Teknologi
  13. * @author Olav Frengstad <olav@fwt.no>
  14. * @license ./LICENSE BSD 3-Clause License
  15. * @since 0.2
  16. */
  17. /**
  18. * Finds the key based on case-insensetiv search
  19. *
  20. * @param string|int $needle The needle to search for
  21. * @param array $haystack Array to search through
  22. * @return mixed|false The key or false if not found
  23. */
  24. function array_cs_search ( $needle, array $haystack )
  25. {
  26. $keys = array_keys( $haystack );
  27. for ( $i = 0, $c = count($haystack); $i < $c; $i++ )
  28. {
  29. if ( 0 === strcasecmp($needle, $keys[$i]) )
  30. {
  31. return $keys[$i];
  32. }
  33. }
  34. unset( $haystack );
  35. return false;
  36. }
  37. /**
  38. * Compute the difference 2 arrays based on index
  39. *
  40. * @param array $arr The initial array
  41. * @param [ array $arr2 Additional array]
  42. * @return array The difference
  43. */
  44. function array_diff_seq ( array $arr )
  45. {
  46. $argv = func_get_args();
  47. $argc = func_num_args();
  48. $ret = array();
  49. for ( $i = 0, $c = count($arr); $i < $c; $i++, $equal = false )
  50. {
  51. for ( $x = 1; $x < $argc; $x++ )
  52. {
  53. if ( !isset($argv[$x][$i]) || $argv[$x][$i] !== $arr[$i] )
  54. {
  55. $ret[$i] = $arr[$i];
  56. }
  57. }
  58. }
  59. unset( $argc, $argv, $x, $i );
  60. return $ret;
  61. }
  62. /**
  63. * Merge arrays with matching keys
  64. *
  65. * @param array $arr The initial array
  66. * @param [ array $arr2 Additional array]
  67. * @param [ array ... ]
  68. * @return array
  69. */
  70. function array_merge_distinct( array $arr )
  71. {
  72. $argv = func_get_args();
  73. array_walk($argv, function ($array) use (&$arr) {
  74. $keys = array_keys( $array );
  75. for ( $i = 0, $c = count($keys); $i < $c; $i++ )
  76. if ( array_key_exists($keys[$i], $arr ) )
  77. $arr[$keys[$i]] = $array[$keys[$i]];
  78. });
  79. unset( $argv );
  80. return $arr;
  81. }
  82. /**
  83. * Filter an array based on value matching regex
  84. *
  85. * @param array $array The array to filter
  86. * @param string $regex The regex to filter on
  87. * @param array The filtered array
  88. */
  89. function array_filter_regex ($array, $regex) {
  90. return array_filter($array, function ($value) use ($regex) {
  91. return 0 !== preg_match($regex, $value);
  92. });
  93. }
  94. /**
  95. * Filter an array based on key matching regex
  96. *
  97. * @param array $array The array to filter
  98. * @param string $regex The regex to filter on
  99. * @param array The filtered array
  100. */
  101. function array_filter_regex_key ($array, $regex) {
  102. return array_intersect_key($array, array_flip(array_filter_regex(array_keys($array), $regex)));
  103. }
  104. /**
  105. * Filter out all elements from $array that does not inherit from $type
  106. *
  107. * @param array $array A array containing possible matches
  108. * @param string $type The class name to check for
  109. * @return array The filtered array
  110. */
  111. function array_filter_class ($array, $type) {
  112. return array_filter($array, function ($value) use ($type) { return is_a($value, $type); });
  113. }