PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/koowa/helper/array.php

https://bitbucket.org/organicdevelopment/nooku-framework
PHP | 168 lines | 89 code | 15 blank | 64 comment | 17 complexity | 0c8d2552191b78c9421e4fe1eef1888f MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Koowa_Helper
  5. * @subpackage Array
  6. * @copyright Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  7. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  8. * @link http://www.nooku.org
  9. */
  10. /**
  11. * Array helper
  12. *
  13. * @author Johan Janssens <johan@nooku.org>
  14. * @package Koowa_Helper
  15. * @subpackage Array
  16. * @static
  17. */
  18. class KHelperArray
  19. {
  20. /**
  21. * Typecast each element of the array. Recursive (optional)
  22. *
  23. * @param array Array to typecast
  24. * @param string Type (boolean|int|float|string|array|object|null)
  25. * @param boolean Recursive
  26. * @return array
  27. */
  28. public static function settype(array $array, $type, $recursive = true)
  29. {
  30. foreach($array as $k => $v)
  31. {
  32. if($recursive && is_array($v)) {
  33. $array[$k] = self::settype($v, $type, $recursive);
  34. } else {
  35. settype($array[$k], $type);
  36. }
  37. }
  38. return $array;
  39. }
  40. /**
  41. * Count array items recursively
  42. *
  43. * @param array
  44. * @return int
  45. */
  46. public static function count(array $array)
  47. {
  48. $count = 0;
  49. foreach($array as $v){
  50. if(is_array($v)){
  51. $count += self::count($v);
  52. } else {
  53. $count++;
  54. }
  55. }
  56. return $count;
  57. }
  58. /**
  59. * Merge two arrays recursively
  60. *
  61. * Matching keys' values in the second array overwrite those in the first array, as is the
  62. * case with array_merge, i.e.:
  63. *
  64. * KHelperArray::merge(array('key' => 'org value'), array('key' => 'new value'));
  65. * => array('key' => array('new value'));
  66. *
  67. * Parameters are passed by reference, though only for performance reasons. They're not
  68. * altered by this function and the datatypes of the values in the arrays are unchanged.
  69. *
  70. * @param array
  71. * @param array
  72. * @return array An array of values resulted from merging the arguments together.
  73. */
  74. public static function merge( array &$array1, array &$array2 )
  75. {
  76. $args = func_get_args();
  77. $merged = array_shift($args);
  78. foreach($args as $array)
  79. {
  80. foreach ( $array as $key => &$value )
  81. {
  82. if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) ){
  83. $merged [$key] = self::merge ( $merged [$key], $value );
  84. } else {
  85. $merged [$key] = $value;
  86. }
  87. }
  88. }
  89. return $merged;
  90. }
  91. /**
  92. * Extracts a column from an array of arrays or objects
  93. *
  94. * @param array List of arrays or objects
  95. * @param string The index of the column or name of object property
  96. * @return array Column of values from the source array
  97. */
  98. public static function getColumn(array $array, $index)
  99. {
  100. $result = array();
  101. foreach($array as $k => $v)
  102. {
  103. if(is_object($v)) {
  104. $result[$k] = $v->$index;
  105. } else {
  106. $result[$k] = $v[$index];
  107. }
  108. }
  109. return $result;
  110. }
  111. /**
  112. * Utility function to map an array to a string
  113. *
  114. * @static
  115. * @param array|object The array or object to transform into a string
  116. * @param string The inner glue to use, default '='
  117. * @param string The outer glue to use, default ' '
  118. * @param boolean
  119. * @return string The string mapped from the given array
  120. */
  121. public static function toString($array = null, $inner_glue = '=', $outer_glue = ' ', $keepOuterKey = false)
  122. {
  123. $output = array();
  124. if($array instanceof KConfig)
  125. {
  126. $data = array();
  127. foreach($array as $key => $item)
  128. {
  129. $data[$key] = (string) $item;
  130. }
  131. $array = $data;
  132. }
  133. if(is_object($array)) {
  134. $array = (array) KConfig::unbox($array);
  135. }
  136. if(is_array($array))
  137. {
  138. foreach($array as $key => $item)
  139. {
  140. if(is_array($item))
  141. {
  142. if($keepOuterKey) {
  143. $output[] = $key;
  144. }
  145. // This is value is an array, go and do it again!
  146. $output[] = KHelperArray::toString($item, $inner_glue, $outer_glue, $keepOuterKey);
  147. }
  148. else $output[] = $key.$inner_glue.'"'.str_replace('"', '&quot;', $item).'"';
  149. }
  150. }
  151. return implode($outer_glue, $output);
  152. }
  153. }