PHP Utils: 复制/批量移除 数组内元素

1. 将$source内的元素复制到$target数组内
2. 将$target数组内 拥有与 $source相同key的元素移除.

	/**
	 * 将指定Array内的元素复制到目标Array中, 如果具有相同key, 则会被覆盖.
	 **/
	public static function copyArrayItems($source, $target) {
		foreach($source as $sourceItemKey=>$sourceItemValue) {
			$target[$sourceItemKey] = $sourceItemValue;
		}
		return $target;
	}

	/**
	 * 移除$target数组中中带有 $source中元素相同key的元素.
	 * @param $source
	 * @param $target
	 * @return unknown_type
	 */
	public static function removeArrayItems($source, $target) {
		foreach($source as $sourceKey => $sourseValue) {
			unset($target[$sourseValue]);
		}
		return $target;
	}

 

有关数组的操作: http://liguoliang.com/2010/php-array-简单操作小结/

This entry was posted in PHP and tagged , , . Bookmark the permalink.

One Response to PHP Utils: 复制/批量移除 数组内元素

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>