Extension:Arrays

ArrayExtension defines an additional set of parser functions that operate on arrays.

Functions
This module defines these functions:

arraydefine
This function constructs an array (identified by 'key') using a list of 'values' separated by the 'delimiter'. The variable can be accessed by other functions later.

Syntax:

Note(s):
 * 'values' is a list of strings separated by 'delimiter'
 * the resulting array is an array of strings.
 * the default delimiter is ',' if not specified, a delimiter can be (i) a string (the white-spaces surrounding delimiter will be trimmed) or (ii) a perl regular expression (for advanced user only), e.g. '/\s*,\s*/' (see preg_split)
 * this function shows nothing
 * users can define an empty array, or reset an array by leaving the 'values' empty (see example)

Example(s):

arrayprint
This function prints the values of an array in customizable format. For each value in the array, print 'subject' where all occurrences of 'search' is replaced with the value, deliminated by 'delimiter'

Syntax:

Note(s):
 * 'subject' now accept wiki link, templates and parser functions. new

Example(s):

arraysize
This function returns the size (number of elements) of an array. See: http://www.php.net/manual/en/function.count.php

Syntax:

Example(s):

arraysearch
This function returns the index of the first occurrence of the 'value' in the array (identified by 'key'), and returns '-1' when failed. when 'yes' and 'no' specified, print value of yes if found, and value of no otherwise. See: http://www.php.net/manual/en/function.array-search.php

Syntax:

Example(s):

arrayindex
This function print the value of an array (identified by 'key') at position 'index'.

Syntax:

Note(s):
 * invalid index (non-number, out of bound) will result in printing an empty string.
 * the index is 0-based, i.e. the first element's index is 0.

Example(s):

arraysort
This function sorts an array in the following order.
 * none (default) - no sort
 * desc - in descending order (see: http://www.php.net/manual/en/function.sort.php)
 * asce - in ascending order (see: http://www.php.net/manual/en/function.rsort.php)
 * random - in random order (see: http://www.php.net/manual/en/function.array-rand.php)

Syntax:

Note(s):
 * each array element is a string

Example(s):

arrayunique
This function converts an array (identified by 'key') into a set (no duplicated members, no empty element). see: http://www.php.net/manual/en/function.array-unique.php

Syntax:

Example(s):

arrayreset
This function free-up all defined arrays.

Syntax:

arraymerge
This function merges values of two arrayes (identified by 'key1' and 'key2') into a new array (identified by 'key'). See: http://www.php.net/manual/en/function.array-merge.php

Syntax:

Note(s):
 * this merge is different from array_merge offered by PHP because it merges values instead of keys

Example(s):

arrayslice
This function extract a sub-array from an array (identified by 'key1') into a new array (identified by 'key'). See: http://www.php.net/manual/en/function.array-slice.php

Syntax:

Note(s):
 * offset indicates starting point of slice, it can be (i) non-negative number (ii) negative number for backwards index (e.g. the last element of the array's offset is -1)
 * length indicates how many element to extract. If it is omitted, then the sequence will have everything from offset up until the end of the array.

Example(s):

arrayintersect
This function computes the set theoretic intersection of two given arrays (identified by 'key1' and 'key2'), and the result array is identified by 'key'. See: http://www.php.net/manual/en/function.array-intersect.php

Syntax:

Note(s):
 * this is a set operator, i.e., the returned array is a set without duplicated values.

Example(s):

arrayunion
This function computes the set theoretic union of two given arrays (identified by 'key1' and 'key2'), and the result array is identified by 'key'. See: http://www.php.net/manual/en/function.array-union.php

Syntax:

Note(s):
 * this is a set operator, i.e., the returned array is a set without duplicated values.
 * similar to arraymerge, this union operation works on values.

Example(s):

arraydiff
This function computes the (set theoretic) difference of two given arrays (identified by 'key1' and 'key2'), and the result array is identified by 'key'. The returned array (identified by 'key') is a set that contains elements in an array (identified by 'key1') but not in the other array (identified by 'key2'). See: http://www.php.net/manual/en/function.array-diff.php

Syntax:

Note(s):
 * this is a set operator, i.e., the returned array is a set without duplicated values.
 * this function can be used to test sub-class relation

Example(s):

Installation
This extension has been tested on MediaWiki 1.13+ and PHP 5. But most of its functions are compatible with PHP 4, so it is reasonable to try this extension on earlier versions of MediaWiki. Please use the following instructions to install this extension.

1. Install source code svn co http://smwbp.googlecode.com/svn/trunk/mediawiki/extensions/ArrayExtension/
 * you can copy the source code from SVN, and put them under "WIKI-PATH/extentions/ArrayExtension"
 * or if you have shell access, you can install using svn

2. Append the following to LocalSettings.php (near the bottom) of your MediaWiki installation:

work with Extension:SemanticMediaWiki
ArrayExtension allows users to populate an array using a SMW query result.

Example A. to create a list of instances of the class 'Color'

Example B. to create a unique list of values of property 'has color'

Example C. to deal with 2D array generated by SWM query (e.g. n-ary property) given a 2D array "red;#da2021, yellow;#fcff00, green;#00ff00"

1. create an array 'colors'

2. split the first element of 'colors' into another array 'colors0'

Note(s)
 * semantic query parameters
 * 'limit=1000' option is used to exhaust all returned results of the semantic query
 * 'sep=,' option is used to set the separator for entries of the results
 * 'mainlable=-' option cut of the page column

iteratively access array elements
We can iteratively access elements of an array using with the following code:

below is the expected output:

for live examples, follow this URL

work with Extension:SemanticQueryFormTool
ArrayExtension allows users to populate an array using a SemanticQueryFormTool query result.

to create a list of instances of the class 'Color'

source: thanks for zehetner@molgen.mpg.de

related MediaWiki extensions

 * ParserFunctions
 * VariablesExtension
 * StringFunctions

Change Log
ArrayExtension 1.1.2 has been tested on MediaWiki versions 1.13.x.

History: - update #arraysearch, now support offset and preg regular expression - updated #arraydefine with better support to foreach. now users can embed wiki templates, parser functions. - update #arraysearch, now allows customized output upon found/non-found by specifying additional parameters - update #arraydefine: replacing 'explode' by 'preg_split', and we now allow delimitors to be (i) a string; or (ii) a perl regular expressnion pattern, sourrounded by '/', e.g. '/..blah.../' - update #arrayprint, change parameters from "prefix","suffix" to a "template", and users can replace a substring in the template with array value, similar to arraymap in semantic forms - update #arrayunique, empty elements will be removed - update #arraysort: adding "random" option to make the array of values in random order - add #arrayreset to free all defined arrays for memory saving - add #arrayslice to return an array bounded by start_index and length. - add #arraysearch. now we can return the index of the first occurence of an element, return -1 if not found - remove #arraymember, obsoleted by #arraysearch - remove #arraypush, obsoleted by #arraydefine and #arraymerge - remove #arraypop, obsoleted by #arrayslice - add safty check code to avoid unset parameters
 * Feb 09, 2009 -- v1.1.2
 * Feb 08, 2009 -- v1.1.1
 * Feb 05, 2009 -- v1.1
 * Feb 01, 2009 -- v1.0.3 -- fixed bug on arrayunique, array_unique (PHP function) only makes values unique but does not update array index. (arraydefine is also affected)
 * Jan 28, 2009 -- v1.0.2 -- fix #arraypop to support pop multiple elements; add #arrayindex
 * Jan 27, 2009 -- v1.0.1 -- fix #arraydefine to support defining an empty string
 * Jan 27, 2009 -- v1.0 -- First release(alpha).