Class RSArrayUtil

Description

A Utility Class for Simple and Advanced Arrays Operations.

General argument order for all methods: The array to operate on is allways the first argument.

Located in /code/framework/RSArrayUtil/RSArrayUtil.php (line 64)


	
			
Method Summary
void &addElement (Array &$array, mixed &$element)
void &addElements (Array &$array, mixed &$elements, mixed $element)
boolean allTrue (Array $array, [boolean $strictComparison = true])
int countElement (Array $array, mixed $element, [RSComparator $comparator = false])
void getAllCombinations (Array $elements, mixed $comparator, [boolean $ignoreOrder = true], [boolean $allowMultipleOccurrencesInCombination = false], [Array $combinations = array()], [mixed $maxLength = false], [Array $lastCombination = array()])
Array getElementsFromMultiDimArrayByKey (Array $array, mixed $key)
boolean hasElement (Array $array, mixed $element, [RSComparator $comparator = false])
boolean hasElements (Array $array, Array $elements, [RSComparator $comparator = false])
Array removeDuplicates (Array &$array, [RSComparator $comparator = false])
String toString (Array $array, [String $elementSeperator = "
"
], [String $format = "%key:%val"])
Methods
addElement (line 177)

Returns the array passed as first argument with the element passed as second argument added.

This method can be used in two ways. The first one is to pass the array by value and save the return type of addElement:


1 $array = RSArrayUtil::addElement($array,$element);

The second way is to pass the array by reference (an not care about the return value):


1 RSArrayUtil::addElement($array, $element);

Both ways have the same result!

void &addElement (Array &$array, mixed &$element)
  • Array $array: The array, the element passed as second argument should be added to.
  • mixed $element: The element to add. Can be any type.
addElements (line 201)

Returns the array passed as first argument with the elements contained in the array passed as second argument added.

This method can be used in two ways. The first one is to pass the first array by value and save the return type of addElements:


1 $array = RSArrayUtil::addElements($array,$array2);

The second way is to pass the first array by reference (an not care about the return value):


1 RSArrayUtil::addElements($array, $array2);

Both ways have the same result! Please note that the keys of the added elements are not preserved.

void &addElements (Array &$array, mixed &$elements, mixed $element)
  • Array $array: The array, the element passed as second argument should be added to.
  • mixed $element: The element to add. Can be any type.
allTrue (line 100)

Returns true if all elements of the array passed as argument evaluate true.

  • return: True if all elements are true.
boolean allTrue (Array $array, [boolean $strictComparison = true])
  • Array $array: The array to validate.
  • boolean $strictComparison: Whether to perfor a comparison using === or ==. The default is ===.
countElement (line 293)

Returns the number of occurrences of a value in an array.

The comparison is done by the === operator. An other comparison can be forced by passing an instance of the class RSComparator or a class that extends it.

int countElement (Array $array, mixed $element, [RSComparator $comparator = false])
  • Array $array: The array to operate on.
  • mixed $element: The element to search for in the array passed as first argument. Can by any type.
  • RSComparator $comparator: An instance of the class RSComparator or a class that extends it. This argument is optional.
getAllCombinations (line 368)

Returns all possible combinations of the elements of an array.

If the array would contain three elements, holding the integers 1, 2 and 3 the returned array of combinations would have a length of 39 and start off like this (if configured not to ignore the order and to allow duplicates):

    void getAllCombinations (Array $elements, mixed $comparator, [boolean $ignoreOrder = true], [boolean $allowMultipleOccurrencesInCombination = false], [Array $combinations = array()], [mixed $maxLength = false], [Array $lastCombination = array()])
    • Array $elements: Array that contains all possible elements.
    • mixed $comparator: An instance of the class RSComparator or a class that extends it.
    • boolean $ignoreOrder: Whether to ignore the order of the elements when checking if such a combination already exists. This argument is optional. The default is the true.
    • boolean $allowMultipleOccurrencesInCombination: Whether to allow multiple occurrences in one combination. This argument is optional. The default is false.
    • Array $combinations: An array of combinations already computed. This argument is used internally because this method is recursive.
    • Array $lastCombination: Do not use this argument! It is only internally needed because this method is recursive. This argument is optional. The default is array().
    • mixed $maxLength: The maximum number of elements in a combination. If set to false the maximum is the number of possible elements passed in as first argument. This argument is optional. The default is false.
    getElementsFromMultiDimArrayByKey (line 149)

    Returns an array containing all elements from the multidimensional array that where referenced by a key equal to $key.

    Example:


    1 $array = array(
    2 array(
    3 'a' => 'zero',
    4 'b' => 'one',
    5 'c' => 'two'
    6 ),
    7 array(
    8 'a' => 'one',
    9 'b' => 'two',
    10 'c' => 'three'
    11 ),
    12 array(
    13 'a' => 'two',
    14 'b' => 'three',
    15 'c' => 'four'
    16 )
    17 );
    18 $aArray = RSArrayUtil::getElementsFromMultiDimArrayByKey($array, 'a');
    19 //will echo '0:zero, 1:one, 2:two'
    20 echo RSArrayUtil::toString($aArray, ', ', '%key:%val');

    • return: An array containing all found elements.
    Array getElementsFromMultiDimArrayByKey (Array $array, mixed $key)
    • Array $array: A multidimensional array.
    • mixed $key: The key to search for.
    hasElement (line 245)

    Returns true if the the array passed as first argument contains the element passed as second argument.

    The comparison is done by http://www.php.net/in_array without type checking by default. An other comparison can be forced by passing an instance of the class RSComparator or a class that extends it. Simple example:


    1 $array = array('a', 'b', 'c');
    2 if (RSArrayUtil::hasElement($array, 'b')) {
    3 echo 'b is contained in the array';
    4 }
    Complex example using a comperator:

    1 class TestElement {
    2 var $hash = null;
    3 function TestElement($hash)
    4 {
    5 $this->hash = $hash;
    6 }
    7 function hashCode()
    8 {
    9 return $this->hash;
    10 }
    11 }
    12 $array = array(new TestElement('a'), new TestElement('b'), new TestElement('c'));
    13 if (RSArrayUtil::hasElement($array, new TestElement('c'), new RSComparator())) {
    14 echo 'c is contained in the array';
    15 }

    • return: True if the array passed as first argument contains the element passed as second argument.
    • see: RSComparator
    boolean hasElement (Array $array, mixed $element, [RSComparator $comparator = false])
    • Array $array: The array to operate on.
    • mixed $element: The element to search for in the array passed as first argument. Can by any type.
    • RSComparator $comparator: An instance of the class RSComparator or a class that extends it. This argument is optional.
    hasElements (line 272)

    Returns true if the the array passed as first argument contains all elements of the array passed as second argument.

    The comparison is handled by hasElement which uses http://www.php.net/in_array without type checking by default. An other comparison can be forced by passing an instance of the class RSComparator or a class that extends it.

    boolean hasElements (Array $array, Array $elements, [RSComparator $comparator = false])
    • Array $array: The array to search in.
    • Array $elements: An array of elements to search for in the array passed as first argument.
    • RSComparator $comparator: An instance of the class RSComparator or a class that extends it. This argument is optional.
    removeDuplicates (line 323)

    Removes all duplicates from an array.

    A new array with all duplicates removed is returned. The comparison is handled by hasElement which uses http://www.php.net/in_array without type checking by default. An other comparison can be forced by passing an instance of the class RSComparator or a class that extends it. This method iterates over the array passed as argument and copies all elements to the new array that are not already contained in it.

    Array removeDuplicates (Array &$array, [RSComparator $comparator = false])
    • Array $array: The array to operate on.
    • RSComparator $comparator: An instance of the class RSComparator or a class that extends it. This argument is optional.
    toString (line 77)

    Returns a literal representation of an array.

    This method is very flexible because it allows the programmer to specify the format of the key value pairs and the seperation character between these pairs. If the value is an object and has a toString method, that method is called to express the value of this array element.

    • return: A literal representation of the array passed as first argument.
    String toString (Array $array, [String $elementSeperator = "
    "
    ], [String $format = "%key:%val"])
    • Array $array: The array to operate on.
    • String $elementSeperator: The String to insert between the array elements. The default is an HTML break
      .
    • String $format: The output format of one key value pair (on array element). Can be any string. %key is the placholder for the key and %val is the placeholder for the value. The default is "%key:%val".

    Documentation generated on Mon, 8 Dec 2003 13:10:38 +0100 by phpDocumentor 1.2.3