帮助:解析函数/arraydefine
{{#arraydefine: 数组名 | 数据字符串 | 分隔符 | 选项 }}
- 官方文档:Extension:Arrays
arraydefine
定义临时的数组变量,供页面内使用。出自扩展 Arrays BWIKI和各大Wiki平台广泛使用此扩展。<br>在遥远的未来,它可能与Mediawiki新的并行解析器不兼容,请参阅扩展主页了解更多信息。。
比如把字符串 apple,banana,orange 拆分建立数组,后续可以按需做集合运算(交、并、差集)和排序、去重等操作,最后可以输出指定位置的元素或整体按指定格式输出。
通常用于需要批量处理数据的场景,如根据 SMW 查询结果生成表格、从模板参数构建列表、批量生成图鉴条目等重复性结构。
语法
{{#arraydefine: 数组名 | 数据 | 分隔符 | 选项 }}
参数
- 数组名
- 要定义的变量名(区分大小写)
- 数据
- 要拆分的内容字符串,留空则定义空数组。可以是固定文本、模板参数、查询结果等
- 分隔符
- 默认逗号 (
,),可使用正则。留空则不拆分,整个字符串作为单个元素.
- 选项:
- 多个选项用逗号分隔,如
unique, sort=asc,支持:
unique:去除重复元素sort:指定排序方式,可选参数:none不排序(默认)asc升序asce升序desc降序random随机reverse倒序
print指定输出格式:list:用顿号连接输出(如"a、b、c")pretty:“人类友好”格式(如"a、b 和 c")
singleempty:保留唯一的空字符串元素
效果
- 输出:若设置
print选项,按指定格式输出数组内容;否则不输出任何字符。 - 效果:创建或更新指定名称的数组变量,在当前页面解析生命周期内有效
示例
基础用法
| 代码 | 效果 |
|---|---|
{{#arraydefine:a|red}}
|
定义数组a:red |
{{#arraydefine:b|orange,red ,yellow, yellow}}
|
定义数组b:orange、red、yellow、yellow |
{{#arraydefine:c}}
|
定义空数组 |
{{#arraydefine:d|apple, pear; orange|/\s*[;,]\s*/}}
|
定义数组d:apple、pear、orange |
选项用法
| 代码 | 效果 |
|---|---|
|
|
去重:orange、red、yellow |
|
|
去重+降序:yellow、red、orange |
{{#arraydefine:g|orange,red,yellow|,|print=list}}
|
立即输出:orange、red、yellow |
{{#arraydefine:h|orange,red,yellow|,|print=pretty}}
|
输出格式:orange、red和yellow |
使用场景
适用于需要批量处理和重复利用数据的场景:
数据预处理:从 SMW 查询、模板参数等获取数据后,统一去重、排序、格式化。
批量生成内容:配合循环结构(如 arrayprint)生成表格行、列表项、图鉴条目等
条件筛选:先定义数组,再用其他 Arrays 函数(如 arrayslice、arraysearch)进行筛选和操作
临时数据存储:在复杂模板中临时保存中间结果,避免重复计算
相比直接在 wikitext 中硬编码,使用 arraydefine 可以:
- 动态处理不确定的数据源
- 提高代码可维护性和复用性
- 简化复杂的条件逻辑
底层代码
来自MediaWiki及其扩展的源代码,运行在服务端。此处仅供快速查阅,便于更充分的挖掘其“特性”。
/** mediawiki-extensions-Arrays-REL1_37 ExtArrays.php
* Define an array by a list of 'values' deliminated by 'delimiter',
* the delimiter should be perl regular expression pattern
* usage:
* {{#arraydefine:arrayid|values|delimiter|options}}
*
* http://us2.php.net/manual/en/book.pcre.php
* see also: http://us2.php.net/manual/en/function.preg-split.php
*/
public static function pf_arraydefine(
Parser &$parser,
$arrayId,
$value = null,
$delimiter = '/\s*,\s*/',
$options = ''
) {
if ( !isset( $arrayId ) ) {
return '';
}
$out = '';
$array = [];
$trimDone = false; // whether or not we can be sure that all array elements are trimmed
// normalize
$delimiter = trim( $delimiter );
if ( $value === null ) {
// no element set, not even an empty one
$array = [];
} else {
// fill array with user input:
if ( $delimiter === '' ) {
// whole input one element, also takes care of special case empty '' value and 'unique' option set
$array = [ $value ];
$trimDone = true;
} else {
// if no regex delimiter given, build one:
if ( !self::isValidRegEx( $delimiter ) ) {
$delimiter = '/\s*' . preg_quote( $delimiter, '/' ) . '\s*/';
$trimDone = true; // spaces are part of the delimiter now
}
$array = preg_split( $delimiter, $value );
}
// trim all values before unique if still necessary, otherwise unique might not work correctly
if ( !$trimDone ) {
$array = self::sanitizeArray( $array );
}
// now parse the options, and do posterior process on the created array
$arrayOptions = self::parse_options( $options );
// make it unique if option is set
if ( array_key_exists( 'unique', $arrayOptions ) ) {
// unique like the parser function would do it
$array = self::array_unique( $array );
}
// if 'singleempty' is NOT set, {{#arraydefine:a|}} will be empty.
// by default this would give an empty array (due to historical as well as usability reasons)
if ( !array_key_exists( 'singleempty', $arrayOptions ) ) {
// there is no other uncomplicated way than this to define a single empty elemented array currently!
if ( count( $array ) === 1 && $array[0] === '' ) {
$array = [];
}
}
// sort array if the option is set
if ( array_key_exists( 'sort', $arrayOptions ) ) {
$array = self::arraySort( $array, self::array_value( $arrayOptions, 'sort' ) );
}
// print the array upon request
switch ( self::array_value( $arrayOptions, 'print' ) ) {
case 'list':
// simple list output
$out = implode( self::$mDefaultSep, $array );
break;
case 'pretty':
global $wgLang;
$out = $wgLang->listToText( $array );
break;
}
}
self::get( $parser )->setArray( $arrayId, $array );
return $out;
}
处理流程:
- 分隔符处理:若非正则表达式,自动转换为正则并去除首尾空格
- 数组拆分:使用
preg_split按分隔符切分字符串 - 元素清理:对所有元素执行 trim 操作
- 去重处理:若设置
unique选项,调用array_unique - 空数组处理:仅有单个空字符串元素的数组会被转为空数组(除非设置
singleempty) - 排序处理:根据
sort选项执行相应排序算法 - 输出处理:若设置
print选项,格式化输出数组内容 - 存储数组:将处理后的数组存入解析器上下文,供后续使用
排序基于相关PHP函数,如sort、rsort、array-rand、array-reverse

沪公网安备 31011002002714 号