Previous Next
MagicQuotesUtil RSD

RSArrayUtil

Array Utility Class

Table of Contents

Introduction

RSArrayUtil was initially developed for use in RSDEngine - The Rapid and Secure Development Engine. RSArrayUtil is static class. General argument order for all methods is to pass the array to operate on as first argument.

RSArrayUtil::toString

This method converts an array to a string. The first argument is the array to operate on. The second is a string - the element seperator. The default is an HTML BR-Tag. The third argument is a string that defines the format of each key-value pair. You can use the placeholders %key for the element key and %val for the element value. The default is "%key:%val".


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3
4 $conditions = array(
5 "f_login_id" => 5,
6 "f_document_id" => 87,
7 "f_statusid" => 101,
8 "f_name" => "john"
9 );
10
11 //will print f_login_id = '5' AND f_document_id = '87' AND f_statusid = '101' AND f_name = 'john'
12 echo RSArrayUtil::toString($conditions, ' AND ', "%key = '%val'");
13 ?>

RSArrayUtil::allTrue

This method returns true if all elements of the array passed as argument evaluate to true. By passing the constant RSARRAYUTIL_LAZY_COMPARISONS as second argument it is possible to force lazy comparision using the '==' operator (and not the '===' operator). Example with (default) strict comparison:


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 $array = array(
4 'a' == 'a',
5 0 == false,
6 1 !== true
7 );
8 //will echo 'TRUE'
9 echo RSArrayUtil::allTrue($array) ? "TRUE" : "FALSE";
10 ?>
Example with lazy comparison:

1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 $array = array(
4 'a' == 'a',
5 0 == false,
6 1 !== true,
7 1,
8 'not an empty string'
9 );
10 //will echo 'TRUE'
11 echo RSArrayUtil::allTrue($array, RSARRAYUTIL_LAZY_COMPARISONS) ? "TRUE" : "FALSE";
12 ?>

RSArrayUtil::allTrue

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


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

RSArrayUtil::addElement

Adds the element passed as second argument to the array passed as first argument. Here is an example:


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3
4 $array = array('one', 'two');
5 $element = 'three';
6 RSArrayUtil::addElement($array, $element);
7 //will echo '0:one, 1:two, 2:three'
8 echo RSArrayUtil::toString($array, ', ', '%key:%val');
9 ?>

RSArrayUtil::addElements

Adds the elements passed as second argument to the array passed as first argument. Here is an example:


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3
4 $array1 = array('one', 'two');
5 $array2 = array('three', 'four');
6 RSArrayUtil::addElements($array1, $array2);
7 //will echo '0:one, 1:two, 2:three, 3:four'
8 echo RSArrayUtil::toString($array1, ', ', '%key:%val');
9
10 ?>
Please note that the keys of the added elements are not preserved:

1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3
4 $array1 = array('one' => 'one', 'two' => 'two');
5 $array2 = array('three' => 'three', 'four' => 'four');
6 RSArrayUtil::addElements($array1, $array2);
7 //will echo 'one:one, two:two, 0:three, 1:four'
8 echo RSArrayUtil::toString($array1, ', ', '%key:%val');
9 ?>

RSArrayUtil::hasElement

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. Here is a simple example:


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 $array = array('a', 'b', 'c');
4 if (RSArrayUtil::hasElement($array, 'b')) {
5 echo 'b is contained in the array';
6 }
7 ?>
Or a little more complex using a comperator:

1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 class TestElement {
4 var $hash = null;
5 function TestElement($hash)
6 {
7 $this->hash = $hash;
8 }
9 function hashCode()
10 {
11 return $this->hash;
12 }
13 }
14 $array = array(new TestElement('a'), new TestElement('b'), new TestElement('c'));
15 if (RSArrayUtil::hasElement($array, new TestElement('c'), new RSComparator())) {
16 echo 'The element c is contained in the array';
17 }
18 ?>
The most complex example I could think of:

1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 class TestElement {
4 var $hash = null;
5 function TestElement($hash)
6 {
7 $this->hash = $hash;
8 }
9 function hashCode()
10 {
11 return $this->hash;
12 }
13 }
14
15 $array = array(
16 array(new TestElement(0), new TestElement(1), new TestElement(2)),
17 array(new TestElement(1), new TestElement(3), new TestElement(2)),
18 array(new TestElement(2), new TestElement(3), new TestElement(4))
19 );
20 if (
21 RSArrayUtil::hasElement(
22 $array,
23 array(
24 new TestElement(1),
25 new TestElement(2),
26 new TestElement(3)
27 ),
28 new RSArrayComparator(new RSComparator(), RSARRAYUTIL_IGNORE_ORDER)
29 )
30 ) {
31 echo 'The multidimensional array contains an array that contains the ' .
32 ' elements new TestElement(1), new TestElement(2) and ' .
33 ' new TestElement(3) - but probably not in this order.';
34 }
35 ?>

RSArrayUtil::hasElements

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 RSArrayUtil::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.


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 $array = array('a', 'b', 'c', 'd');
4 //will echo 'TRUE'
5 echo RSArrayUtil::hasElements($array, array('a', 'c')) ? "TRUE" : "FALSE";
6 ?>
And again an complex example:

1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3
4 class TestElement {
5 var $hash = null;
6 function TestElement($hash)
7 {
8 $this->hash = $hash;
9 }
10 function toString()
11 {
12 return $this->hash;
13 }
14 }
15
16 $array = array(
17 array(new TestElement(0), new TestElement(1), new TestElement(2)),
18 array(new TestElement(1), new TestElement(3), new TestElement(2)),
19 array(new TestElement(2), new TestElement(3), new TestElement(4))
20 );
21 if (
22 RSArrayUtil::hasElements(
23 $array,
24 array(
25 array(
26 new TestElement(2),
27 new TestElement(3),
28 new TestElement(1)
29 ),
30 array(
31 new TestElement(4),
32 new TestElement(2),
33 new TestElement(3)
34 ),
35 ),
36 new RSArrayComparator(new RSToStringComparator(), RSARRAYUTIL_IGNORE_ORDER)
37 )
38 ) {
39 echo 'The multidimensional array contains an the two arrays - but probably the ' .
40 'TestElement are in a different order in the multidimensional array.';
41 }
42 ?>

RSArrayUtil::countElement

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.


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 $array = array('a', 'a', 'a', 'b', 'c');
4
5 //well echo 3
6 echo RSArrayUtil::countElement($array, 'a');
7 ?>
And again an complex example:

1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3
4 class TestElement {
5 var $hash = null;
6 function TestElement($hash)
7 {
8 $this->hash = $hash;
9 }
10 function toString()
11 {
12 return $this->hash;
13 }
14 }
15
16 $array = array(
17 array(new TestElement(1), new TestElement(2), new TestElement(3)),
18 array(new TestElement(0), new TestElement(1), new TestElement(2)),
19 array(new TestElement(1), new TestElement(3), new TestElement(2)),
20 array(new TestElement(2), new TestElement(3), new TestElement(4)),
21 array(new TestElement(3), new TestElement(2), new TestElement(1)),
22 );
23
24 //will echo 3
25 echo RSArrayUtil::countElement(
26 $array,
27 array(
28 new TestElement(2),
29 new TestElement(3),
30 new TestElement(1)
31 ),
32 new RSArrayComparator(new RSToStringComparator(), RSARRAYUTIL_IGNORE_ORDER)
33 );
34 ?>

RSArrayUtil::removeDuplicates

Removes all duplicates from an array. A new array with all duplicates removed is returned. The comparison is handled by RSArrayUtil::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. Here is a simple example:


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 $array = array('a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd');
4
5 //removeDuplicates does NOT modify the
6 //passed argument but will return a new array!
7 $array = RSArrayUtil::removeDuplicates($array);
8
9 //will echo '0:a, 1:b, 2:c, 3:d, 4:e'
10 echo RSArrayUtil::toString($array, ', ', '%key:%val');
11 ?>
This one is a little more complex:

1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3
4 class TestElement {
5 var $hash = null;
6 function TestElement($hash)
7 {
8 $this->hash = $hash;
9 }
10 function toString()
11 {
12 return $this->hash;
13 }
14 }
15 $array = array(
16 array(new TestElement(0), new TestElement(1), new TestElement(2)),
17 array(new TestElement(1), new TestElement(3), new TestElement(2)),
18 array(new TestElement(2), new TestElement(3), new TestElement(4)),
19 array(new TestElement(2), new TestElement(3), new TestElement(5)),
20 array(new TestElement(1), new TestElement(0), new TestElement(2)),
21 array(new TestElement(3), new TestElement(1), new TestElement(2)),
22 array(new TestElement(3), new TestElement(2), new TestElement(4))
23 );
24
25 //RemoveDuplicates does NOT modify the
26 //passed argument but will return a new array!
27 //The array will be shortend to a length of 4 elements.
28 $array = RSArrayUtil::removeDuplicates(
29 $array,
30 new RSArrayComparator(new RSToStringComparator(), RSARRAYUTIL_IGNORE_ORDER)
31 );
32
33 //will echo 4
34 echo count($array);
35 ?>

RSArrayUtil::getAllCombinations

Returns all possible combinations of the elements of an array passed as first argument. The second argument must be an instance of the class {@see RSComparator} or a class that extends it. With the third argument you can specify 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. With the fourth argument you can specify whether to allow multiple occurrences in one combination. This argument is optional. The default is false.


1 <?php
2 require_once('RSArrayUtil/RSArrayUtil.php');
3 //will echo 7
4 echo count(
5 RSArrayUtil::getAllCombinations(
6 array(1, 2, 3),
7 new RSPrimitiveComparator()
8 )
9 );
10
11 //will echo 15
12 echo count(
13 RSArrayUtil::getAllCombinations(
14 array(1, 2, 3),
15 new RSPrimitiveComparator(),
16 RSARRAYUTIL_RESPECT_ORDER
17 )
18 );
19
20 //will echo 39
21 echo count(
22 RSArrayUtil::getAllCombinations(
23 array(1, 2, 3),
24 new RSPrimitiveComparator(),
25 RSARRAYUTIL_RESPECT_ORDER,
26 true
27 )
28 );
29 ?>

Previous Next
MagicQuotesUtil RSD

Documentation generated on Mon, 8 Dec 2003 13:09:43 +0100 by phpDocumentor 1.2.3