<?php

# Convert stuff in a database in to an AA

$in = $argv[1];
$value = $argv[2];

# Clean the array index; only letters, numbers, _, and / allowed
function qaa_index_clean($key) {
	$out = preg_replace('/[^0-9A-Za-z_\/áéíóúñÑ]/',"",$key);
	$out = preg_replace('/\/\//',"",$out); # Hack: No null indexes
	return $out;
}

# Inset a single row (element) in to the AA from the database
# This is a hack that uses eval and the global $data array
function qaa_insert_element($key, $value) {
	global $data;
	$key = qaa_index_clean($key);
	$key = preg_replace('/\//','"]["',$key);
	$key = '$data["' . $key . '"] = $value;';
	eval($key);
}

# Export a single value in the AA; quick and dirty "eval" hack
function qaa_fetch_value($key) {
	global $data;
	$key = qaa_index_clean($key);
	$key = preg_replace('/\//','"]["',$key);
	$key = '$value = $data["' . $key . '"];';
	eval($key);
	return $value;
}

# Show on the standard output all of the keys and values in database
# form; recursive
function qaa_fetch_all_data($string, $in) {
	foreach($in as $key => $value) {
		if(is_array($value)) {
			if($string != "") {
				qaa_fetch_all_data($string ."/". $key,$value);
			} else {
				qaa_fetch_all_data($key,$value);
			}
		} else {
			print "'" . $string . "/" . $key . "' ";
			print "'" . $value . "'\n";
		}
	}	
}

qaa_insert_element($argv[1], $argv[2]);
qaa_fetch_all_data("",$data);

?>
