Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  /**
   3   * The Horde_Array:: class provides various methods for array manipulation.
   4   *
   5   * Copyright 2003-2017 Horde LLC (http://www.horde.org/)
   6   *
   7   * See the enclosed file LICENSE 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      public static 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  
  39          /* If no key to sort by is specified, use the first key of the
  40           * first element. */
  41          if (is_null($key)) {
  42              $keys = array_keys(reset($array));
  43              $key = array_shift($keys);
  44          }
  45  
  46          /* Call the appropriate sort function. */
  47          $helper = new Horde_Array_Sort_Helper();
  48          $helper->key = $key;
  49          $function = $dir ? 'reverseCompare' : 'compare';
  50          if ($assoc) {
  51              uasort($array, array($helper, $function));
  52          } else {
  53              usort($array, array($helper, $function));
  54          }
  55      }
  56  
  57      /**
  58       * Given an HTML type array field "example[key1][key2][key3]" breaks up
  59       * the keys so that they could be used to reference a regular PHP array.
  60       *
  61       * @param string $field  The field name to be examined.
  62       * @param string &$base  Will be set to the base element.
  63       * @param array &$keys   Will be set to the list of keys.
  64       *
  65       * @return boolean  True on sucess, false on error.
  66       */
  67      public static function getArrayParts($field, &$base, &$keys)
  68      {
  69          if (!preg_match('|([^\[]*)((\[[^\[\]]*\])+)|', $field, $matches)) {
  70              return false;
  71          }
  72  
  73          $base = $matches[1];
  74          $keys = explode('][', $matches[2]);
  75          $keys[0] = substr($keys[0], 1);
  76          $keys[count($keys) - 1] = substr($keys[count($keys) - 1], 0, strlen($keys[count($keys) - 1]) - 1);
  77          return true;
  78      }
  79  
  80      /**
  81       * Using an array of keys iterate through the array following the
  82       * keys to find the final key value. If a value is passed then set
  83       * that value.
  84       *
  85       * @param array &$array  The array to be used.
  86       * @param array &$keys   The key path to follow as an array.
  87       * @param array $value   If set the target element will have this value set
  88       *                       to it.
  89       *
  90       * @return mixed  The final value of the key path.
  91       */
  92      public static function getElement(&$array, array &$keys, $value = null)
  93      {
  94          if (count($keys)) {
  95              $key = array_shift($keys);
  96              return isset($array[$key])
  97                  ? self::getElement($array[$key], $keys, $value)
  98                  : false;
  99          }
 100  
 101          if (!is_null($value)) {
 102              $array = $value;
 103          }
 104  
 105          return $array;
 106      }
 107  
 108      /**
 109       * Returns a rectangle of a two-dimensional array.
 110       *
 111       * @param array   $array   The array to extract the rectangle from.
 112       * @param integer $row     The start row of the rectangle.
 113       * @param integer $col     The start column of the rectangle.
 114       * @param integer $height  The height of the rectangle.
 115       * @param integer $width   The width of the rectangle.
 116       *
 117       * @return array  The extracted rectangle.
 118       */
 119      public static function getRectangle(array $array, $row, $col, $height,
 120                                          $width)
 121      {
 122          $rec = array();
 123          for ($y = $row; $y < $row + $height; $y++) {
 124              $rec[] = array_slice($array[$y], $col, $width);
 125          }
 126          return $rec;
 127      }
 128  
 129      /**
 130       * Given an array, returns an associative array with each element key
 131       * derived from its value.
 132       * For example:
 133       *   array(0 => 'foo', 1 => 'bar')
 134       * would become:
 135       *   array('foo' => 'foo', 'bar' => 'bar')
 136       *
 137       * @param array $array  An array of values.
 138       *
 139       * @return array  An array with keys the same as values.
 140       */
 141      public static function valuesToKeys(array $array)
 142      {
 143          return $array
 144              ? array_combine($array, $array)
 145              : array();
 146      }
 147  }