Version: current

      1 <?php
      2 /**
      3  * Library of static methods for manipulating arrays.
      4  * @package sapphire
      5  * @subpackage misc
      6  */
      7 class ArrayLib {
      8
      9     /**
     10      * Inverses the first and second level keys of an associative
     11      * array, keying the result by the second level, and combines
     12      * all first level entries within them.
     13      *
     14      * Before:
     15      * <example>
     16      * array(
     17      *  'row1' => array(
     18      *      'col1' =>'val1',
     19      *      'col2' => 'val2'
     20      *  ),
     21      *  'row2' => array(
     22      *      'col1' => 'val3',
     23      *      'col2' => 'val4'
     24      *  )
     25      * )
     26      * </example>
     27      *
     28      * After:
     29      * <example>
     30      * array(
     31      *  'col1' => array(
     32      *      'row1' => 'val1',
     33      *      'row2' => 'val3',
     34      *  ),
     35      *  'col2' => array(
     36      *      'row1' => 'val2',
     37      *      'row2' => 'val4',
     38      *  ),
     39      * )
     40      * </example>
     41      *
     42      * @param array $arr
     43      * @return array
     44      */
     45     static function invert($arr) {
     46         if(!$arr) return false;
     47         $result = array();
     48
     49         foreach($arr as $columnName => $column) {
     50             foreach($column as $rowName => $cell) {
     51                 $result[$rowName][$columnName] = $cell;
     52             }
     53         }
     54
     55         return $result;
     56     }
     57
     58     /**
     59      * Return an array where the keys are all equal to the values
     60      *
     61      * @param $arr array
     62      * @return array
     63      */
     64     static function valuekey($arr) {
     65         $newArr = array();
     66         foreach($arr as $val) {
     67             $newArr[$val] = $val;
     68         }
     69         return $newArr;
     70     }
     71
     72     /**
     73      * @todo Improve documentation
     74      */
     75     static function array_values_recursive($arr) {
     76        $lst = array();
     77        foreach(array_keys($arr) as $k){
     78          $v = $arr[$k];
     79          if (is_scalar($v)) {
     80              $lst[] = $v;
     81          } elseif (is_array($v)) {
     82              $lst = array_merge( $lst,
     83                self::array_values_recursive($v)
     84              );
     85          }
     86        }
     87        return $lst;
     88     }
     89
     90     /**
     91      * Filter an array by keys (useful for only allowing certain form-input to be saved).
     92      *
     93      * @param $arr array
     94      * @param $keys array
     95      * @return array
     96      */
     97     static function filter_keys($arr, $keys)
     98     {
     99         foreach ($arr as $key => $v) {
    100             if (!in_array($key, $keys)) {
    101                 unset($arr[$key]);
    102             }
    103         }
    104         return $arr;
    105     }
    106
    107     /**
    108      * Determines if an array is associative by checking
    109      * for existing keys via array_key_exists().
    110      * @see http://nz.php.net/manual/en/function.is-array.php#76188
    111      *
    112      * @param array $arr
    113      * @return boolean
    114      */
    115     static function is_associative($arr) {
    116         if(is_array($arr) && ! empty($arr)) {
    117             for($iterator = count($arr) - 1; $iterator; $iterator--) {
    118                 if (!array_key_exists($iterator, $arr)) return true;
    119             }
    120             return !array_key_exists(0, $arr);
    121         }
    122         return false;
    123     }
    124
    125     /**
    126      * Recursively searches an array $haystack for the value(s) $needle.
    127      * Assumes that all values in $needle (if $needle is an array) are at
    128      * the SAME level, not spread across multiple dimensions of the $haystack.
    129      *
    130      * @param mixed $needle
    131      * @param array $haystack
    132      * @param boolean $strict
    133      * @return boolean
    134      */
    135     static function in_array_recursive($needle, $haystack, $strict = false) {
    136         if(!is_array($haystack)) return false; // Not an array, we've gone as far as we can down this branch
    137
    138         if(in_array($needle, $haystack, $strict)) return true; // Is it in this level of the array?
    139         else {
    140             foreach($haystack as $obj) { // It's not, loop over the rest of this array
    141                 if(self::in_array_recursive($needle, $obj, $strict)) return true;
    142             }
    143         }
    144
    145         return false; // Never found $needle :(
    146     }
    147
    148 }
    149
    150 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -