Array.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * The Horde_Array:: class provides various methods for array manipulation.
  4. *
  5. * Copyright 2003-2013 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @author Michael Slusarz <slusarz@horde.org>
  11. * @author Marko Djukic <marko@oblo.com>
  12. * @author Jan Schneider <jan@horde.org>
  13. * @category Horde
  14. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  15. * @package Util
  16. */
  17. class Horde_Array
  18. {
  19. /**
  20. * Sorts an array on a specified key. If the key does not exist,
  21. * defaults to the first key of the array.
  22. *
  23. * @param array &$array The array to be sorted, passed by reference.
  24. * @param string $key The key by which to sort. If not specified then
  25. * the first key is used.
  26. * @param integer $dir Sort direction:
  27. * 0 = ascending (default)
  28. * 1 = descending
  29. * @param boolean $assoc Keep key value association?
  30. */
  31. static public function arraySort(array &$array, $key = null, $dir = 0,
  32. $assoc = true)
  33. {
  34. /* Return if the array is empty. */
  35. if (empty($array)) {
  36. return;
  37. }
  38. /* If no key to sort by is specified, use the first key of the
  39. * first element. */
  40. if (is_null($key)) {
  41. $keys = array_keys(reset($array));
  42. $key = array_shift($keys);
  43. }
  44. /* Call the appropriate sort function. */
  45. $helper = new Horde_Array_Sort_Helper();
  46. $helper->key = $key;
  47. $function = $dir ? 'reverseCompare' : 'compare';
  48. if ($assoc) {
  49. uasort($array, [$helper, $function]);
  50. } else {
  51. usort($array, [$helper, $function]);
  52. }
  53. }
  54. /**
  55. * Given an HTML type array field "example[key1][key2][key3]" breaks up
  56. * the keys so that they could be used to reference a regular PHP array.
  57. *
  58. * @param string $field The field name to be examined.
  59. * @param string &$base Will be set to the base element.
  60. * @param array &$keys Will be set to the list of keys.
  61. *
  62. * @return boolean True on sucess, false on error.
  63. */
  64. static public function getArrayParts($field, &$base, &$keys)
  65. {
  66. if (!preg_match('|([^\[]*)((\[[^\[\]]*\])+)|', $field, $matches)) {
  67. return false;
  68. }
  69. $base = $matches[1];
  70. $keys = explode('][', $matches[2]);
  71. $keys[0] = substr($keys[0], 1);
  72. $keys[count($keys) - 1] = substr($keys[count($keys) - 1], 0, strlen($keys[count($keys) - 1]) - 1);
  73. return true;
  74. }
  75. /**
  76. * Using an array of keys iterate through the array following the
  77. * keys to find the final key value. If a value is passed then set
  78. * that value.
  79. *
  80. * @param array &$array The array to be used.
  81. * @param array &$keys The key path to follow as an array.
  82. * @param array $value If set the target element will have this value set
  83. * to it.
  84. *
  85. * @return mixed The final value of the key path.
  86. */
  87. static public function getElement(&$array, array &$keys, $value = null)
  88. {
  89. if (count($keys)) {
  90. $key = array_shift($keys);
  91. return isset($array[$key])
  92. ? self::getElement($array[$key], $keys, $value)
  93. : false;
  94. }
  95. if (!is_null($value)) {
  96. $array = $value;
  97. }
  98. return $array;
  99. }
  100. /**
  101. * Returns a rectangle of a two-dimensional array.
  102. *
  103. * @param array $array The array to extract the rectangle from.
  104. * @param integer $row The start row of the rectangle.
  105. * @param integer $col The start column of the rectangle.
  106. * @param integer $height The height of the rectangle.
  107. * @param integer $width The width of the rectangle.
  108. *
  109. * @return array The extracted rectangle.
  110. */
  111. static public function getRectangle(array $array, $row, $col, $height,
  112. $width)
  113. {
  114. $rec = [];
  115. for ($y = $row; $y < $row + $height; $y++) {
  116. $rec[] = array_slice($array[$y], $col, $width);
  117. }
  118. return $rec;
  119. }
  120. /**
  121. * Given an array, returns an associative array with each element key
  122. * derived from its value.
  123. * For example:
  124. * array(0 => 'foo', 1 => 'bar')
  125. * would become:
  126. * array('foo' => 'foo', 'bar' => 'bar')
  127. *
  128. * @param array $array An array of values.
  129. *
  130. * @return array An array with keys the same as values.
  131. */
  132. static public function valuesToKeys(array $array)
  133. {
  134. return $array
  135. ? array_combine($array, $array)
  136. : [];
  137. }
  138. }