社区文档构建进行中,欢迎编辑。社区答疑群(非官方):717421103

全站通知:

帮助:解析函数/arrayunion

来自WIKI实验室WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

Arrayunion是一个解析函数。帮助:解析函数页列出了所有解析函数的说明。

arrayunion

合并数组。出自扩展 Arrays BWIKI和各大Wiki平台广泛使用此扩展。<br>在遥远的未来,它可能与Mediawiki新的并行解析器不兼容,请参阅扩展主页了解更多信息。

将多个数组合并,存到新数组中。合并时不保留重复值。

语法

{{#arrayunion: 新数组名 | 数组1 | 数组2 | ... | 数组n }}

示例

有数组:

  • x : a、b、c、c
  • y : b、c、d
  • z : 1、2、3

合并数组:

{{#arrayunion:r|x|y}} → a、b、c、d
{{#arrayunion:r|y|x|z}} → b、c、d、a、1、2、3
{{#arrayunion:r|x|z|y}} → a、b、c、1、2、3、d

底层代码

/**
 * Merge values two arrayes identified by arrayid1 and arrayid2 into a new array identified by arrayid_new.
 * This merge differs from array_merge of php because it merges values.
 *
 * Usage:
 *    {{#arraymerge:arrayid_new |array1 |array2 |... |array n}}
 *    See: http://www.php.net/manual/en/function.array-merge.php
 */
public static function pfObj_arraymerge( &$parser, $frame, $args ) {
	self::get( $parser )->multiArrayOperation( $frame, $args, __FUNCTION__, false );
	return '';
}

private function multi_arraymerge( $array1, $array2 ) {
	// keys will not be re-organized
	return array_merge( $array1, $array2 );
}


/**
 * Base function for operations with multiple arrays given thru n parameters
 * $operationFunc expects a function name prefix (suffix 'multi_') with two parameters
 * $array1 and $array2 which will perform an action between $array1 and $array2 which
 * will result into a new $array1. There can be 1 to n $hash2 in the whole process.
 *
 * Note: This function is similar to that of Extension:HashTables.
 *
 * @since 2.0
 *
 * @param PPFrame $frame
 * @param array $args
 * @param string $operationFunc name of the function calling this. There must be a counterpart
 *        function with prefix 'multi_' which should have two parameters. Both parameters
 *        will receive an array, the function must return the result array of the processing.
 * @param bool $runFuncOnSingleArray whether the operationFunc function should be run in case
 *        only one array id is given. If not, the original array will end up in the new array.
 */
protected function multiArrayOperation( PPFrame $frame, array $args, $operationFunc, $runFuncOnSingleArray = true ) {
	$args = array_values( $args );
	$numArgs = count( $args );

	$lastArray = null;
	$operationRan = false;
	$finalArrayId = trim( $frame->expand( $args[0] ) );
	$operationFunc = 'multi_' . preg_replace( '/^pfObj_/', '', $operationFunc );

	// For all arrays given in parameters 2 to n (ignore 1 because this is the name of the new array)
	for ( $i = 1; $i < $numArgs; $i++ ) {
		$argArrayId = trim( $frame->expand( $args[ $i ] ) );

		// ignore all tables which do not exist
		if ( $this->arrayExists( $argArrayId ) ) {
			$argArray = $this->getArray( $argArrayId );
			if ( $lastArray === null ) {
				// first valid array, process together with second...
				$lastArray = $argArray;
			} else {
				// second or later hash table, process with previous:
				$lastArray = $this->{ $operationFunc }( $lastArray, $argArray ); // perform action between last and current array
				$operationRan = true;
			}
		}
	}

	// in case no array was given at all:
	if ( $lastArray === null ) {
		$lastArray = [];
	}

	global $egArraysCompatibilityMode;

	if ( !$operationRan && $egArraysCompatibilityMode
		&& $operationFunc !== 'multi_arraymerge' // only exception was 'arraymerge'
	) {
		/*
			* COMPATIBILITY-MODE:
			* Before version 2.0 we didn't create a new array in case only one array was given.
			* The only exception was 'arraymerge' which did duplicate the array.
			*/
		return;
	}

	// if the operation didn't run because there was only one or no array:
	if ( !$operationRan && $runFuncOnSingleArray ) {
		$lastArray = $this->{ $operationFunc }( $lastArray );
	}

	// re-organize all keys since some 'multi_' functions will preserve keys!
	$lastArray = array_merge( $lastArray );

	$this->setArray( $finalArrayId, $lastArray );
}
代码逻辑:
  • 代码给出了多个函数,但实际上逻辑较为简单。
  • Arrays扩展中涉及多数组操作的核心方法是 multiArrayOperation。它对多个数组执行指定操作(op),即:arr1=op(arr1, arr2), arr1=op(arr1, arr3), ... , arr1=op(arr1, arrN)
  • 函数 pfObj_arraymerge 是解析函数的主体,它通过 multiArrayOperation 函数多次调用 multi_arraymerge,将参数中所有的数组合并到第一个数组中,即:arr1=array_merge(arr1, arr2), arr1=array_merge(arr1, arr3), ... , arr1=array_merge(arr1, arrN)

实际用例

一些Wiki使用了相关特性,如下所示这个静态列表可能在下列页面更改后过时仅供批判性参考