<?php

function dbValueUpdateCount(&$db_values,$i)
{
$db_values[$i][3]++;
}

function dbValueFind(&$db_values,$str='',&$values_cnt,$i)
{
$id  = false;
foreach ($db_values as $key=>$value)
	{
	if ($value[0] == $str) {
		dbValueUpdateCount($db_values,$key);
		$id = $key;
		break;
		}
//	if ($value === $str) {return $key;}
	}
if ($id===false)
	{
	$id = dbValueAdd($db_values,$values_cnt,array($str,$i));
	}
return $id;
}

function dbValueAdd(&$db_values,&$n,$data)
{
$data[] = $n;
$data[] = 0;
$db_values[$n] = $data;
//$db_values[$n] = $str;
++$n;
return $n - 1;
}

function sortCmpValue($a,$b)
{
return $b[0]>$a[0];
}

function sortCmpCol($a,$b)
{
return $b[1]>$a[1];
}

function sortCmpCount($a,$b)
{
return $b[3]>$a[3];
}




$file = 'zasuvky_bn13.csv';
$file = 'data-inp\int-tel-seznam-x5.csv';
$file = 'data-inp\Int. tel. seznam 2016-x6.csv';
$file = 'data-inp\tel-seznam.csv';
 

$db_keys   = array();
$db_values = array();
$db_map    = array();
$db_filters = array();
$values_cnt = 0;

//for ($i=0; $i<20; $i++)
//{
//$db_data[]  = array($i,'');
//}
//dbValueAdd($db_values,$values_cnt,0,'');
dbValueAdd($db_values,$values_cnt,array('',0,0));

$csv = array(
'sep_row'    => "\n",
'sep_col'    => ';',
'enclosure'  => '"',
'escape'     => '"',
'row_length' => 1000
);
$row_length = 1000;	//chars with csv escape
if (($handle = fopen($file, "r")) !== FALSE)
	{
	$row  = array();
	$bool = false;
	$j = 0;
	while (($row = fgetcsv($handle, $csv['row_length'], $csv['sep_col'], $csv['enclosure'], $csv['escape'])) !== FALSE)	
		{
//var_dump($row);
//exit;
//if (count($db_map)>5) break;

		if ($bool)
			{
			$row_table = array();
			$value = $j.'';
			$id    = dbValueFind($db_values,$value,$values_cnt,0);
//			if ($id===false)
//				{
//				$id = dbValueAdd($db_values,$values_cnt,array($value,$i));
//				}
			$row_table[] = $id;	//row id
		        for ($i=0; $i<count($row); ++$i)
				{
				$value = trim($row[$i]);
//				if ($i>0) {$value = substr(sha1($value),0,strlen($value));}	//hash str
				$id    = dbValueFind($db_values,$value,$values_cnt,$i+1);
//				if ($id===false)
//					{
//					$id = dbValueAdd($db_values,$values_cnt,array($value,$i));
//					}
				$row_table[] = $id;
				}
			if (count($row_table)>1)
				{
				$db_map[] = $row_table;
				}
			++$j;//row id
			}
		else	{
			array_unshift($row,'id');	//row id
			$db_keys = $row;
			$bool = true;
			}
		}
	fclose($handle);
	}

//usort($db_values,'sortCmpValue');
// sort by words count + remap
//$db_values[0][3]--; // remove one '';
//var_dump($db_values);

usort($db_values,'sortCmpCount');
$cnv = array();
foreach ($db_values as $key => $value)
	{
	$cnv[$value[2]] = $key;
//	$cnv[] = array($value[2],$key);
	}
//var_dump($cnv);

foreach ($db_map as $key => $value)	//remap
	{
	foreach ($value as $key2 => $value2)
		{
		//if (isset($cnv[$value2]) )
		//	{
//			$v = $value2;
		$v = $cnv[$value2];
		$db_map[$key][$key2] = $v;
		if (!isset($db_filters[$key2]))
			{
			$db_filters[$key2] = array();
			}
		//var_dump($db_filters[$key2]);
		if (!in_array($v,$db_filters[$key2]))
			{
			$db_filters[$key2][] = $v;
			}
		//	}
		//else {echo $value2.'<br>';}
		}
	}

//var_dump($db_keys);
//var_dump($db_values);
//echo count($db_values);
//var_dump($db_map);


// convert to table
/*
$x = array();
for ($i=0; $i<3; $i++)
	{
	$row = $db_map[$i];
	foreach ($row as $key=>$value)
		{
//		$row[$key] = $db_values[$value];
		$row[$key] = $db_values[$value][0];
		}
	$x[] = $row;
	}
var_dump($x);
*/
/*
$x = array();
for ($i=0; $i<3; $i++)
	{
	$row = $db_map[$i];
	$x[] = $row;
	}
var_dump($x);
*/

function jsEscapeString($str)
{
$str = preg_replace('~[\']~' ,'\\$1',$str);
$str = preg_replace('~[\\n]~','\\$1',$str);
return $str;
}

function toJS(&$db_keys,&$db_values,&$db_map,&$db_filters)
{
//var_dump($db_keys);
$str  = 'var db_keys, db_values, db_map;';
$str .=  "\n\ndb_keys   = [\n";
$nl = "\n";
$nl = "";
$bool = false;
foreach ($db_keys as $key => $value)
	{
	$str .= "'".jsEscapeString($value)."',".$nl;
	$bool = true;
	}
if ($bool==true) {$str  = substr($str,0,-1);}
$str .= "\n];\n\ndb_values = [\n";
$bool = false;
foreach ($db_values as $key => $value)
	{
	$str .= "'".jsEscapeString($value[0])."',".$nl;
//	$str .= "'".jsEscapeString($value)."',";
	$bool = true;
	}
if ($bool==true) {$str  = substr($str,0,-1);}
$str .= "\n];\n\ndb_map    = [\n";
$bool = false;
foreach ($db_map as $key => $value)
	{
	if (is_array($value))
		{
		$str .= "[".implode(",",$value)."],".$nl;
		$bool = true;
		}
	}
if ($bool==true) {$str  = substr($str,0,-1);}
$str .= "\n];\n\ndb_filters= [\n";
foreach ($db_filters as $key => $value)
	{
	if (is_array($value))
		{
		$str .= "[".implode(",",$value)."],".$nl;
		$bool = true;
		}
	}
if ($bool==true) {$str  = substr($str,0,-1);}
$str .= "\n];";
return $str;
}

echo '<pre>'.toJS($db_keys,$db_values,$db_map,$db_filters);
?>

<!--pre style="position:absolute; top:0;left:400px">
array (size=3)
  0 => 
    array (size=20)
      0 => string '1' (length=1)
      1 => string 'sw120 - Cisco Catalyst 4506' (length=27)
      2 => string 'Te1/1' (length=5)
      3 => string 'sw100 - c6513' (length=13)
      4 => string 'Te13/1' (length=6)
      5 => string 'sm' (length=2)
      6 => string '10GE, arma, uplink' (length=18)
      7 => string '' (length=0)
      8 => string 'o' (length=1)
      9 => string 'switch-uplink' (length=13)
      10 => string 'trunk' (length=5)
      11 => string '' (length=0)
      12 => string '' (length=0)
      13 => string '' (length=0)
      14 => string '' (length=0)
      15 => string '' (length=0)
      16 => string '' (length=0)
      17 => string '' (length=0)
      18 => string '' (length=0)
      19 => string '' (length=0)
  1 => 
    array (size=20)
      0 => string '1' (length=1)
      1 => string 'sw120 - Cisco Catalyst 4506' (length=27)
      2 => string 'Te1/2' (length=5)
      3 => string '' (length=0)
      4 => string '' (length=0)
      5 => string '' (length=0)
      6 => string '' (length=0)
      7 => string '' (length=0)
      8 => string '' (length=0)
      9 => string '' (length=0)
      10 => string '1' (length=1)
      11 => string '' (length=0)
      12 => string '' (length=0)
      13 => string '' (length=0)
      14 => string '' (length=0)
      15 => string '' (length=0)
      16 => string '' (length=0)
      17 => string '' (length=0)
      18 => string '' (length=0)
      19 => string '' (length=0)
  2 => 
    array (size=20)
      0 => string '1' (length=1)
      1 => string 'sw120 - Cisco Catalyst 4506' (length=27)
      2 => string 'Gi1/3' (length=5)
      3 => string 'sw100 - c6513' (length=13)
      4 => string '' (length=0)
      5 => string 'sm' (length=2)
      6 => string 'uplink, arma, ethe' (length=18)
      7 => string '' (length=0)
      8 => string 'o' (length=1)
      9 => string 'switch-uplink' (length=13)
      10 => string 'trunk' (length=5)
      11 => string '' (length=0)
      12 => string '' (length=0)
      13 => string '' (length=0)
      14 => string '' (length=0)
      15 => string '' (length=0)
      16 => string '' (length=0)
      17 => string '' (length=0)
      18 => string '' (length=0)
      19 => string '' (length=0)
</pre-->