Jun 232013
 

A wrapper class for the standard PHP array. I should probably go ahead and change this to RArrayList and make it implement the RList interface. I’ll mess with that later.

 

 

/**
 * A wrapper for standard PHP array functions plus some helper methods.                                                                                                                                                                                                                                                                                                                         
 *
 * @author Steven Bolin - April 2013 - All rights reserved.
 */
class RArray {
  
  private $array;
 
  public function __construct() {
    $this->array = array();
  }
  
  /**
   * Adds an element to the end of the array and 
   * returns the resulting array
   * @param mixed $element The element to add
   */
  public function add($element) {
    array_push($this->array, $element);
    return $this->array;
  }
  
 /**
  * Adds an element to the end of the array and 
  * returns the resulting array
  * @param mixed $element The element to add
  */
  public function addLast($element) {
    array_push($this->array, $element);
    return $this->array;
  }
  
/**
 * Adds an element to the beginning of the array and 
 * returns the resulting array
 * @param mixed $element The element to add
 */
  public function addFirst($element) {
    array_unshift($this->array, $element);
    return $this->array;
  }
  
/**
 * Returns true if the array contains $element
 * @param mixed The element for which to evaluate the array.
 */
  public function contains($element) {
    return in_array($element, $this->array);
  }
  
/**
 * Removes and returns the first element in the array.
 */
  public function removeFirst() {
    return array_shift($this->array);
  }
  
/**
 * Removes and returns the last element in the array
 */
  public function removeLast() {
    return array_pop($this->array);
  }
  
/**
 * Returns the array
 */
  public function getArray() {
    return $this->array;
  }
  
/**
 * Adds $element at the array index specified by $index and returns the array
 * @param int $index The index at which to add $element
 * @param mixed The element to add at $index
 */
  public function addAt($index, $element) {
    array_splice( $this->array, $index, 0, $element );
    return $this->array;
  }
  
/**
 * Removes the element at the array index given by $index and returns the
 * resulting array.
 * @param int $index The array index of the element to remove
 */
  public function removeAt($index) {
    array_splice($this->array, $index, 1);
    return $this->array;
  }
  
/**
 * Returns the number of elements in the array
 */
  public function size() {
    return count($this->array);
  }
  
/**
 * Searches the array for a given value and returns the corresponding key if successful
 */
  public function findFirst($element) {
    return array_search($element, $this->array);
  }
  
/**
 * Returns TRUE if the given key is set in the array. key can be any value possible for an array index.
 */
  public function isKey($key) {
    return array_key_exists($key, $this->array);
  }
  
/**
 * Removes duplicate values from the array and returns the resulting array
 */
  public function removeDuplicates() {
    $this->array = array_unique($this->array);
    return $this->array;
  }
} // End of class
 Posted by at 10:01 pm