class_mcArray.inc.php 2.67 KB
<?php
/**
 * @todo
 *	könnte in
 *	SEOLinkCache eingesetzt werden
 */
class mcArray {
	public static function returnValueFromKeyPath(array $param_PathAsArray, array $param_Array) {
		$searcharray = $param_Array;
		foreach($param_PathAsArray as $key) {
			if(is_array($searcharray)) {
				if(array_key_exists($key, $searcharray)) {
					$searcharray = $searcharray[$key];
				}
			} else {
			  return false;
			}
		}
		return $searcharray;
	}

	public static $ARRAY_PATTERN = '_#_';
	/**
	 *	transformiert Array mit speziellen String Key zu einem normalen Key
	 *
	 * @param array param_Array -- Array welches transformiert wird
	 * @param string param_pattern -- Pattern für den Schlüssel (im Beispiel '_#_')
	 *
	 * Array
		(
			[bleibt] =>
			[data_#_1_#_Subject] => kein Betreff
		)
	 *
	 *
	 *	Array
		(
			[bleibt] =>
			[data] => Array
				(
					[1] => Array
						(
							[Subject] => kein Betreff
						)

				)
		)
	 */
	public static function transformArrayWithStringKeyToNormalKey(array &$param_Array, $param_pattern=null) {
//		D::show($param_Array, '$param_Array');
		if(is_null($param_pattern)) {
			$param_pattern = mcArray::$ARRAY_PATTERN;
		}

		$tmp_POST = array();
		$tmp_Array = $param_Array;
		foreach($tmp_Array as $post_key => $post_value) {

			if(FALSE!=strpos($post_key, $param_pattern)) {
				$tmp_POST[$post_key] = array();
				$array_key = split($param_pattern, $post_key);

				$tmp_POST_key = null;

				$tmp_POST[$post_key][0]  = array();

				$count_array_key = count($array_key);
				for($i=0; $i<$count_array_key; $i++) {
					if(is_null($tmp_POST_key)) {
						$tmp_POST_key = $array_key[$i];
					}

					if(!array_key_exists($array_key[$i], $tmp_POST[$post_key][$i])) {
						$tmp_POST[$post_key][$i][$array_key[$i]] = array();
					}

					if($count_array_key == $i+1) {
						$tmp_POST[$post_key][$i][$array_key[$i]]  = $post_value;
					} else {
						$tmp_POST[$post_key][$i+1] =& $tmp_POST[$post_key][$i][$array_key[$i]];
					}
				}

				mcArray::addArrayValueToArray($param_Array, $tmp_POST[$post_key][0]);

				unset($param_Array[$post_key]);
			} else {
				$param_Array[$post_key] = $post_value;
			}
		}
	}

	/**
	 *	fügt Tabelle mit Werten zu einer bestehenden Tabelle
	 *
	 *	siehe phpunit/class_mcArray_PHPTest.inc.php 
	 *
	 * @param array $param_Array
	 * @param <type> $param_ArrayValue
	 */
	public static function addArrayValueToArray(array &$param_Array, $param_ArrayValue) {
		
		$value_key = array_keys($param_ArrayValue);

		foreach ($value_key as $key) {
			if(array_key_exists($key, $param_Array)) {
				mcArray::addArrayValueToArray($param_Array[$key], $param_ArrayValue[$key]);
			} else {
				$param_Array[$key] = $param_ArrayValue[$key];
			}
		}

	}
}
?>