memo

memo dayo.

配列変数を関数として出力する

// ==============================================
function ary2php_list ($file_path, $ary_data, $ary_prm=array()) {
    $func_name = $file_path;
	$func_name = basename(realpath($func_name));
	$func_name = str_replace('.php', '', $func_name);
	$func_name = str_replace('.', '_', $func_name);

	foreach ($ary_prm as $idx_1 => $val_1) {
		$ary_prm[$idx_1] = (string)$val_1;
	}

	$str_txt = '';
	$str_txt .= "" . '<?php' . "\n";
	$str_txt .= "" . 'function ' . $func_name . ' ($ary_prm=array()) {' . "\n";
	$str_txt .= "\t" . '$ary_rtn = array();' . "\n";
	$str_txt .= "\n";
	$str_txt .= "\t" . 'foreach ($ary_prm as $idx_1 => $val_1) {' . "\n";
	$str_txt .= "\t\t" . '$ary_prm[$idx_1] = (string)$val_1;' . "\n";
	$str_txt .= "\t" . '}' . "\n";
	$str_txt .= "\n";
	$str_txt .= "\t" . '$ary_rtn = unserialize(gzinflate(base64_decode(' . "'" . base64_encode(gzdeflate(serialize($ary_data))) . "'" . ')));' . "\n";
	$str_txt .= "\n";
	$str_txt .= "\t" . 'ksort($ary_rtn);' . "\n";
	$str_txt .= "\t" . 'return $ary_rtn;' . "\n";
	$str_txt .= "" . '}' . "\n";

	file_put_contents($file_path, $str_txt);
	return chmod($file_path, 0777);
}
// ==============================================
function ary2php_byid ($file_path, $ary_data, $ary_prm=array()) {
	$func_name = $file_path;
	$func_name = basename(realpath($func_name));
	$func_name = str_replace('.php', '', $func_name);
	$func_name = str_replace('.', '_', $func_name);

	foreach ($ary_prm as $idx_1 => $val_1) {
		$ary_prm[$idx_1] = (string)$val_1;
	}

	$str_txt = '';
	$str_txt .= "" . '<?php' . "\n";
	$str_txt .= "" . 'function ' . $func_name . ' ($ary_prm=array()) {' . "\n";
	$str_txt .= "\t" . '$ary_rtn = array();' . "\n";
	$str_txt .= "\n";
	$str_txt .= "\t" . 'foreach ($ary_prm as $idx_1 => $val_1) {' . "\n";
	$str_txt .= "\t\t" . '$ary_prm[$idx_1] = (string)$val_1;' . "\n";
	$str_txt .= "\t" . '}' . "\n";
	$str_txt .= "\n";

	foreach ($ary_data as $idx_1 => $val_1) {
		// make if
		$str_txt .= "\t" . 'if (';
		$str_if = '';
		foreach ($ary_prm as $idx_2 => $val_2) {
			if (strlen($str_if) > 0) {
				$str_if .= ' && ';
			}
			$str_if .= '($ary_prm[' . "'"  . $idx_2 . "'"  . '] === ' . "'" . $val_1[$idx_2] . "')";
		}
		$str_txt .= $str_if;
		unset($str_if);
		$str_txt .= ') {' . "\n";
		// add data
		$str_txt .= "\t\t" . '$ary_rtn[ ] = ';
		$str_txt .= "" . 'unserialize(gzinflate(base64_decode(' . "'" . base64_encode(gzdeflate(serialize($val_1))) . "'" . ')));' . "\n";
		$str_txt .= "\t" . '}' . "\n";
	}

	$str_txt .= "\n";
	$str_txt .= "\t" . 'ksort($ary_rtn);' . "\n";
	$str_txt .= "\t" . 'return $ary_rtn;' . "\n";
	$str_txt .= "" . '}' . "\n";

	file_put_contents($file_path, $str_txt);
	return chmod($file_path, 0777);
}
// ==============================================